alexgr
(Alex)
May 7, 2015, 2:06pm
1
The following really has me scratching my head. I have an array, that looks like this when I var_dump it:
array (size=7)
'private' => string '10031' (length=5)
'temp' => string '10030' (length=5)
'joint' => string '10042' (length=5)
'staples' => string '10102' (length=5)
'jointstaples' => string '10043' (length=5)
'mealplan' => string '43' (length=2)
'jointmealplan' => string '105' (length=3)
…so far so good.
But when I use this:
if ($shopping_list_details['joint']) {
echo "test" ;
}
PHP throws this warning/error:
Undefined index: joint
Here’s the full code below, does it have to with the inner workings perhaps of
ArrayHelper::map
that this won’t work?
Thanks for anyone’s help, I’m really puzzled by this behaviour.
$esluser = \app\models\EarlierShoppingList::find()
->select('esl_id, shoppinglist_types.type')
->innerJoinwith('type0')
->where(['esl_user_id' => $this->user_id])
->asArray()
->all();
$shopping_list_details = \yii\helpers\ArrayHelper::map($esluser, 'type', 'esl_id');
var_dump($shopping_list_details);
if ($shopping_list_details['joint']) {
echo "ok";
}
Regards,
Alex
You are trying to map two attributes: ‘type’ and ‘esl_id’, while your query is only selecting those two attributes: ‘esl_id’ and ‘shoppinglist_types.type’… thus ‘joint’ is not there and will throw an error.
alexgr
(Alex)
May 8, 2015, 8:18pm
3
Thanks for your answer. However, the type columns contains either:
private, or:
joint, or:
staples, or:
temp, or:
jointstaples
So the result of the ArrayMapping is:
array (size=7)
'private' => string '10031' (length=5)
'temp' => string '10030' (length=5)
'joint' => string '10042' (length=5)
'staples' => string '10102' (length=5)
'jointstaples' => string '10043' (length=5)
'mealplan' => string '43' (length=2)
'jointmealplan' => string '105' (length=3)
So the key ‘joint’ exists…that’s why I am at such a loss that:
if ($shopping_list_details['joint']) {
echo "test" ;
}
returns the error…
Hope anyone can shed their light on this, is this is a bug in ArrayHelper::map perhaps or is it a PHP related issue?
Thanks again,
Alex
tampaphp
(tamaphp)
May 9, 2015, 12:50am
4
What’s the error you receive?
No, it does not exist. Look at the "SELECT" in your query:
->select('esl_id, shoppinglist_types.type')
Only these two attributes are in your result set.
alexgr
(Alex)
May 9, 2015, 4:04pm
6
@tampaph , the error I actually get is a warning (have strict settings locally) : Undefined index: joint
@Backslider : note that the select query does an inner join, shoppinglist_types.type is only the column name.
If the “joint” index doesn’t exist, why does var_dumping the output of the ArrayHelper indicate it does?
Thanks,
Alex
argent
(Argent Hun)
May 10, 2015, 1:00pm
7
Try to dump the $shopping_list_details keys and check the keys length:
var_dump(array_keys($shopping_list_details));
maybe you have an extra whitespace character after "joint" text what you do not see (space, tab etc.)
alexgr
(Alex)
May 10, 2015, 5:03pm
8
Argent:
Try to dump the $shopping_list_details keys and check the keys length:
var_dump(array_keys($shopping_list_details));
maybe you have an extra whitespace character after "joint" text what you do not see (space, tab etc.)
That was a good idea, however it does just return 5 characters for joint, so that can’t be it:
array (size=7)
0 => string 'private' (length=7)
1 => string 'temp' (length=4)
2 => string 'joint' (length=5)
3 => string 'staples' (length=7)
4 => string 'jointstaples' (length=12)
5 => string 'mealplan' (length=<img src='http://www.yiiframework.com/forum/public/style_emoticons/default/cool.gif' class='bbc_emoticon' alt='8)' />
6 => string 'jointmealplan' (length=13)
I think I’m going to have to file this as a bug / issue with ArrayHelper, it totally beats me.
Thanks,
Alex
alexgr
(Alex)
May 10, 2015, 6:40pm
10
Yes, it is (excuse the pun):
array (size=5)
0 =>
array (size=3)
'esl_id' => string '10031' (length=5)
'type' => string 'private' (length=7)
'type0' => null
1 =>
array (size=3)
'esl_id' => string '10030' (length=5)
'type' => string 'temp' (length=4)
'type0' => null
2 =>
array (size=3)
'esl_id' => string '10042' (length=5)
'type' => string 'joint' (length=5)
'type0' => null
3 =>
array (size=3)
'esl_id' => string '10102' (length=5)
'type' => string 'staples' (length=7)
'type0' => null
4 =>
array (size=3)
'esl_id' => string '10043' (length=5)
'type' => string 'jointstaples' (length=12)
'type0' => null