CodeIgniter Forums
Finding Value in Array using isset - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Finding Value in Array using isset (/showthread.php?tid=24352)

Pages: 1 2


Finding Value in Array using isset - El Forum - 11-08-2009

[eluser]Fielder[/eluser]
Code:
echo 'Before merge: start';
var_dump($storeArray[2975]);
echo 'end';

$subTotal = $this->rtui_app->subTotal($storeArray);
$storeArray = array_merge($storeArray, $subTotal);

echo 'After merge: start';
var_dump($storeArray[2975]); // This is line 360
echo 'end';

foreach ($stores as $row)
{
    if (!isset($storeArray[$row['store_id']]))
    {
        $storeArray[$row['store_id']] = array(
            'storename' => $row['storename_name'],
            'storenum' => $row['store_number'],
            'newAd_count' => '0',
            'renewAd_count' => '0',
            'totAd_count' => '0',
            'spaces_count' => '0',
            'print' => 'N',
            'lastqtrAd_count' => '0',
            'cancelled' => '0',
            'freeAd_count' => '0',
            'revenue' => '$ '.number_format(0, 2)
        );
    }
}
            
echo 'After foreach Loop: start';
var_dump($storeArray[2975]);
echo 'end';

Quote:Before merge: startarray(11) { ["storename"]=> string(9) "Appletree" ["storenum"]=> string(3) "736" ["newAd_count"]=> int(4) ["renewAd_count"]=> int(4) ["totAd_count"]=> int(8) ["spaces_count"]=> int(10) ["print"]=> string(1) "Y" ["lastqtrAd_count"]=> int(9) ["cancelled"]=> int(1) ["freeAd_count"]=> int(4) ["revenue"]=> string(10) "$ 5,115.87" } endAfter merge: start
A PHP Error was encountered

Severity: Notice

Message: Undefined offset: 2975

Filename: controllers/all_search.php

Line Number: 360
NULL endAfter foreach Loop: startarray(11) { ["storename"]=> string(9) "Appletree" ["storenum"]=> string(3) "736" ["newAd_count"]=> string(1) "0" ["renewAd_count"]=> string(1) "0" ["totAd_count"]=> string(1) "0" ["spaces_count"]=> string(1) "0" ["print"]=> string(1) "N" ["lastqtrAd_count"]=> string(1) "0" ["cancelled"]=> string(1) "0" ["freeAd_count"]=> string(1) "0" ["revenue"]=> string(6) "$ 0.00" } end


So before merge, 2975 is there.
After merge, 2975 is not there.
But after the foreach loop, it adds back in 2975 because it is in $stores.


Finding Value in Array using isset - El Forum - 11-08-2009

[eluser]mah0001[/eluser]
You can remove all the var_dumps. What is the value for the $subTotal?
Code:
$subTotal = $this->rtui_app->subTotal($storeArray);
var_dump($subTotal);

If $subTotal is not an array you cannot merge a string with the array. I think this is causing all the trouble.

Try changing the $subTotal to an array. e.g.
Code:
$subTotal = $this->rtui_app->subTotal($storeArray);
if (!is_array($subTotal) ){
$subTotal=array($subTotal);
}
$storeArray = array_merge($storeArray, $subTotal);



Finding Value in Array using isset - El Forum - 11-08-2009

[eluser]Fielder[/eluser]
It looks like before the merge, the keys are the store_ids (ie 2975). After the merge, the keys in the original $storeArray were changed to 0, 1, 2, 3, etc...

So somehow $storeArray[2975] is being changed to $storeArray[0].

I ran this code
Code:
echo '<hr />';
foreach ($storeArray as $key => $val)
{
    echo $key.'<br />';
}
echo '<hr />';

Maybe this helps.
$storeArray keys before merge:
Quote:2975
2732
2768
3133
2766
2771
2930
2923

$storeArray keys after merge:
Quote: 0
1
2
3
4
5
6
7
Appletree
Fiesta
HEB
Zieglers
Food Town



Finding Value in Array using isset - El Forum - 11-08-2009

[eluser]Fielder[/eluser]
I agree, the merge is causing the problem. Perhaps there's a different way to merge the Appletree, Fiesta, HEB, etc... keys to the existing 2975, 2732, etc... keys? That's all i really want to do is tack those $subTotal keys to the end of $storeArray.

Yes, it's an array.
Code:
$subTotal = $this->rtui_app->subTotal($storeArray);
if (!is_array($subTotal))
{
    echo 'Not Array';
}
else
{
    echo 'Is Array';
}



Finding Value in Array using isset - El Forum - 11-08-2009

[eluser]mah0001[/eluser]
another way to merge arrays:
Code:
$subTotal = $this->rtui_app->subTotal($storeArray);
//$storeArray = array_merge($storeArray, $subTotal);
foreach($subTotal as $key=>$value)
{
$storeArray[$key]=$value;
}



Finding Value in Array using isset - El Forum - 11-08-2009

[eluser]Fielder[/eluser]
*&^$%#... I should have figured that out! It worked, and is simple. I guess I hone in on the error and problem and alternate solutions skip my mind. Thanks for the help.
No reason to use array_merge since this solution worked.

Seems that merging an array with numeric keys with an array with alpha keys screw it up.? Or perhaps merging resets all the keys?

Thanks again mah0001.