B
B
betal2014-09-18 19:20:55
PHP
betal, 2014-09-18 19:20:55

PHP: How to add key => array() element to array?

The task should be very simple, but programming for 5 years in PHP I can not solve it.
There is this pseudocode:

<?php
$brend = array();
//.../
$city   = 6;
$brend  = 11;
//.../
if (!isset($brend[$city])){
    $brend[$city] = array();
}
if (!isset($brend[$city][$brend])){
    $brend[$city][$brend] = array('eff'=>0,'count'=>0);
}
//.../              
$count  = $brend[$city][$brend]['count'];

Actually the error is in the line
>> $brend[$city] = array();
Array to string conversion
But I don't need the value to be a string, I need a multi-level array.
PS.
Possible solutions:
1)
$brend[$city][$brend]['count']=0;
$brend[$city][$brend]['eff']=0;
Will only work if warnings are disabled, but I want to figure out how the developers intended to solve this problem
2)
$brend = array(6 => array( 11 => array('eff'=>0,'count'=>0))) ;
I don't know in advance the values ​​of the $city and $brend variables, and I don't know how many there will be.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey, 2014-09-18
@betal

Well, of course he will swear.
$city = 6; is int.
$brand = 11; is also a variable of type int.
To it, you supposedly make an array 11['6'] - of course, this is not true, it is not an array.
You need to do this $arr[$brend][$city], then the variable $arr[11][6] will be an array.
Video tutorial on arrays , may be useful.

M
Melkij, 2014-09-18
@melkij

$ brand = array();
//.../
$city = 6;
$ brand =11;

?!

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question