Visualizing Array Data - php

I have a problem visualizing arrays greater than one dimension. I did a query on a database table and stored the data in an array, then used mysqli_fetch_array() to create another array. Now this array has the table name and the table data in but I'm having trouble figuring out how to A) access just the data and B) visualize what is actually going on here.
This is the output of print_r($keystore);
Array ( [0] => Array ( [0] => 4 [key_projects] => 4 ) [1] => Array ( [0] => 26 [key_projects] => 26 ) [2] => Array ( [0] => 25 [key_projects] => 25 ) [3] => Array ( [0] => 52 [key_projects] => 52 ) [4] => Array ( [0] => 53 [key_projects] => 53 ) )
What exactly is going on here?

Sometimes it's helpful when developing/debugging code that has arrays, to insert the HTML <pre> tag before and after the print_r() command:
echo "<pre>";
print_r($keystore);
echo "</pre>";
This will force the output into a format similar to John Kugelman's answer (subject to CSS rules). I find, from experience, that each iteration of Array() will be indented, when viewing plain text (i.e. no HTML)

Array
(
[0] => Array ( [0] => 4 [key_projects] => 4 )
[1] => Array ( [0] => 26 [key_projects] => 26 )
[2] => Array ( [0] => 25 [key_projects] => 25 )
[3] => Array ( [0] => 52 [key_projects] => 52 )
[4] => Array ( [0] => 53 [key_projects] => 53 )
)
I've added some whitespace to make the structure of the nested arrays clearer. I didn't change anything aside from adding spaces and newlines.
The outer array contains five entries from [0] to [4]. Each of these entries represents one row from the SQL result set.
for ($keystore as $row) {
print_r($row[0]);
print_r($row['key_projects']);
}
You'll notice the two entries in each $row are redundant. They both have the same value (e.g. 53 for the final entry). What's happening is the data is being returned indexed both by column number (0) and column name (key_projects). You can access the values using whichever one you choose, number or name: $row[0] or $row['key_projects'].

Related

How to Combine Array String As Keys And Number As Value In Php

I have a problem to do the combining between 2 arrays.
In the first array as a string (example array data):
Array
(
[0] => courses
[1] => courses
[2] => courses
[3] => courses
[4] => courses
)
Second array as a value (Example Array values):
Array
(
[0] => 64
[1] => 63
[2] => 62
[3] => 2
[4] => 9
)
After I tried to combine between 2 arrays like this:
$combine = (array_combine($data, $courses))
I can only get a combination result like this:
Array
(
[courses] => 9
)
How can i get the result of combination between the two arrays above, like this?
Array
(
[courses] => 64
[courses] => 63
[courses] => 62
[courses] => 2
[courses] => 9
)
Thanks In Advance.
You are not able to generate your intended output as you can't have multiple of the same key in an array. This is why you are ending up with one entry of 'courses'.
For example this would be do-able:
Array
(
[courses] => array(
[0] => 63
[1] => 63
[2] => 62
[3] => 2
[4] => 9
)
)
You need to change your approach.

multidimensional array using foreach

I'm writing a code for looping out data from an multi dimensional array.
While looping I got confused while getting the details from an array. I have tried several ways for getting but in vain.
Now what I want is to get the values from the key 4 provided in the array.
Array
(
[match1] => Array
(
[4] => Array
(
[0] => Array
(
[0] => Sanjay
[1] => Delhi
[2] => 23
)
[1] => Array
(
[0] => Ram
[1] => Mumbai
[2] => 26
)
)
[5] => Array
(
[0] => Array
(
[0] => Sanjay
[1] => Delhi
[2] => 23
)
[1] => Array
(
[0] => Ram
[1] => Mumbai
[2] => 26
)
)
)
)
Thanks
In order to access the multi dimensional array you need to access via foreach() or directly by using the keys that you have in the print_r() function.
Hence as per your Example you can directly access the variable the you need using the
first array variable along with the key that the first array has.
Consider this array and you need to fetch the first value you can process like this.
print_r($var); resulting in this
Array
(
[match1] => Array
(
[4] => Array
(
[0] => Array
(
[0] => Sanjay
[1] => Delhi
[2] => 23
)
[1] => Array
(
[0] => Ram
[1] => Mumbai
[2] => 26
)
)
)
)
You can retrieve the variable in two methods as follows
Method One:
As the variable that contains the array is $var hence you need to access like this.
In order to fetch the value that the key has you can have $var['match1'][4] and you can apply foreach over to the variable and get the value that it has.
foreach($var['match1'][4] as $inner_value)
{
// Do what ever stuff you need
}
Method Two:
Getting key value 0 - 0 in the array that it has you can code as - $var['match1'][4][0]
Getting key value 1 - 1 in the array that it has you can code as - $var['match1'][4][1]
You can get as much value inside the array as you can with the help of the above two points
Output for both it will be the same as follows
Sanjay Delhi 23
Ram Mumbai 26
It's really very simple. Let's assume the name of your main array is $mainarray. So here is how you can get the array of key 4.
$key4array=$mainarray['match1'][4];
foreach($key4array as $arrayele) {
echo $arrayele[0]." ".$arrayele[1]." ".$arrayele[2]."<br>";
}
Output will be,
Sanjay Delhi 23
Ram Mumbai 26
Access the first level array by match1 key and then 4 as index to get the second level array.

Complicated PHP transpose / pivot

I have the following array :
Array
(
[0] => Array
(
[Name] => first_data
[building] => A
[apt] => 16
)
[1] => Array
(
[Name] => first_data
[building] => B
[apt] => 16
)
[2] => Array
(
[Name] => second_data
[building] => A
[apt] => 17
)
[3] => Array
(
[Name] => second_data
[building] => B
[apt] => 18
)
and I need it to be returned as :
Array
(
[0] => Array
(
[Name] => first_data
[A] => 16
[B] => 16
)
[1] => Array
(
[Name] => second_data
[A] => 17
[B] => 18
)
Any ideas?
BTW the first array has hundreds of entries (not only first_data, but second and etc...) plus it has more than A and B.
Thanks in advance.
Not exactly what you want, but if you instead index the new array by the name, you can do this very easily. If the index number is some kind of ID, you can just create a field for it
foreach ( $oldarray as $index => $piece )
{
$newarray[$piece['Name']] = array($piece['building'] => $piece['apt'])
}
This will give you
Array
(
['first_data'] => Array
(
['A'] => 16,
['B'] => 16
)
['second_data'] => Array
(
['A'] => 17,
['B'] => 18
)
)
Since you have two entries with the same new, when you hit the 2nd loop, it will simply add the other building name. If you can work with this layout, then your solution is very easy, it will take more steps to do it exactly as you showed. If you absolutely have to do it the way you showed, you need extra code to loop through the new array, find the building name, add the key in the correct place, but this will be slower if you have a large amount of data.
In my opinion, the way I presented it is a far easier way to look around the array too. If you wanted to know the apt value for A in "second_data" you can just do
$newarray['second_data']['A']
with your array layout, it would require a loop to search the array for "second_data" because you have no idea where it is.

merging a multi-dimensional array into another multi-dimensional array

this relates to my previous post.
how to create a collection of multi dimensional arrays and not overwrite origional values when new ones are pushed
i am thinking my problem has to do with how i am creating the array. what i'm trying to do is make an array that looks like this
Array
(
[10] => Array
(
[0] => 29
[1] => 36
)
)
into something like this
Array
(
[10] => Array
(
[0] => 29
[1] => 36
)
[20] => Array
(
[0] => 29
[1] => 36
)
[25] => Array
(
[0] => 29
[1] => 36
)
)
the 10, 20, and 25 is the product id where the numbers within those are the selections that were selected on that page (in the link i gave above). so each product would have its own collection selected.
when i use array_push instead of doing what i want it to do the first collection of array as in the first example keep reseting. so if i do my selections on say flyers and add to cart then i go to business cards and do my selections and add to cart the array resets and it becomes like the first example. whatever i try i cant get it to merge below a collection like the second example that i have. i have tried array_merge(),array_push but those dont really work.
Solution:-
If you want to append array elements from the second array to the first array while not overwriting the elements from the first array and not re-indexing, use the + array union operator:
$a = array(10 => array(25,26));
$b = array(22 => array(45,66));
$c = $a + $b;
print_r($c);
Output:-
Array
(
[10] => Array
(
[0] => 25
[1] => 26
)
[22] => Array
(
[0] => 45
[1] => 66
)
)
Hope this helps.

CakePHP Model index array needed, but can't generate from for loop counter. Need model indexed array for this to work

I am working on a function that submits multiple records on various relationship types. The main issue I am running into is the format of the array. In order for my saveAll() to work on my multiple relationships setup, the array needs to be in this format as you can see the models are Keys (first array below).
My main question is: 1) Is it possible to strip the numerical indexes off the second layer of the second array below?
I am returning my input fields like so. You can see the prefixed counter (which I believe is what is creating the numeric index on that second level).
<?php echo $this->Form->input("$i.monthly_cost", array('label' => 'Monthly Cost')); ?>
I am using a for loop counter for the fields. So my question number to is: can this for value be changed to something that will work with Cake's saveAll()?
<?php for ($i = 1; $i <= 2; $i++) { ?>
Example where models are the keys (this is the format I need):
Array
(
[User] => Array
(
[username] => billy
)
[Profile] => Array
(
[sex] => Male
[occupation] => Programmer
)
The only output I can get on my multiple input array (below is the debug() dump)
My actual output is numerically indexed:
Array
(
[Plan] => Array
(
[1] => Array
(
[plan_detail_id] => 36
[monthly_cost] => 0
[dental_cost] => 0
[age_id] => 14
[applicant_id] => 1
[state_id] => 1
)
[2] => Array
(
[plan_detail_id] => 36
[monthly_cost] => 0
[dental_cost] => 0
[age_id] => 2
[applicant_id] => 4
[state_id] => 1
)
)
[1] => Array
(
[1] => Array
(
[Zip] => Array
(
[0] => 487
[1] => 486
[2] => 485
[3] => 484
[4] => 483
)
)
)
[2] => Array
(
[2] => Array
(
[Zip] => Array
(
[0] => 485
[1] => 484
[2] => 483
)
)
)
)
Did you already check out the Set Core Utility Library? This can help you out a lot with array management.

Categories