PHP multidimensional array - php

I have an array declared above the beginning of a for loop as: $array = array();.
Now, in the for loop I start inserting values into it.
At some point I make one of its index as another array as $array[$j]=array();
And insert some values like, $array[$j][$l] = id; and so on.
Now, when I use print_r ($array); inside the loop I get the expected value of the array.
But outside the loop this newly created array (2-D) is getting lost and I am getting only a 1-D array as an output.
Can someone please tell me where the problem could lie?

The following code works properly. Perhaps you are switching your variables as strager suggests.
<?php
$array = array();
for ($i = 0; $i < 10; $i+=1) {
if ($i == 5) {
$array[$i] = array('value 1', 'value 2');
} else {
$array[$i] = $i;
}
}
print_r($array);
?>

Related

Using unset() on an array, but it keeps the value

I'm trying to remove an object from an array if one of his properties is null or empty, this is the code.
The array has been sorted using this function:
function sortArray($c1, $c2)
{
return ($c1->propertyToCheck < $c2->propertyToCheck);
}
In case it changes anything.
$myArray = array();
...
// Add values to the array here
...
usort($myArray,"sortArray");
for($i = 0; $i < count($myArray ); $i++)
{
if(empty($myArray[$i]->propertyToCheck))
{
unset($myArray[$i]);
// var_dump($myArray[$i]) returns NULL
}
}
echo json_encode($myArray);
// Returns the entire array, even with the values that shouldn't be there.
The code is inside a function but the array is created inside said function.
I'm using echo json_encode($myArray) to send the value back in AJAX, but the array sent is the entire array with every object inside it.
The count($myArray) is the "problem".
Once the unset() is "reached" there is one element less in the array and therefore the next call to count($myArray) will return n-1 of the previous iteration -> your loop doesn't get to the end of the array.
You have at least three choices (in ascending order of my preference)
a)
$maxIdx = count($myArray);
for($i = 0; $i < $maxIdx; $i++) {
b)
foreach( $myArray as $key=>$obj ) {
if(empty($obj->propertyToCheck)) {
unset($myArray[$key]);
c)
$myArray = array_filter(
$myArray,
function($e) {
return !empty($e->propertyToCheck);
}
);
(...and many more)
see also: http://docs.php.net/array_filter

Remove a value from an Array in a for loop and replace the previous array with the edited array

i want to eliminate the values from inside the array that has also exist outside the array to form a new array and iterate through the new array to return the first free alphanumeric.
i tried a for loop but it aint working
this is my code
$economyseat = array("5A","5B","5C","5D","5E","5F","6A","6B","6C","6D","6E","6F","7A","7B","7C","7D","7E","7F","8A","8B","8C","8D","8E","8F","9A","9B","9C","9D","9E","9F","10A","10B","10C","10D","10E","10F","11A","11B","11C","11D","11E","11F","12A","12B","12C","12D","12E","12F","13A","13B","13C","13D","13E","13F","14A","14B","14C","14D","14E","14F","15A","15B","15C","15D","15E","15F","16A","16B","16C","16D","16E","16F","17A","17B","17C","17D","17E","17F","18A","18B","18C","18D","18E","18F");
// $seatnumber = 5A,5B,16C & 18A;
$seatlength = count($economyseat);
for ($x = 0; $x < $seatlength; $x++)
{
$seat = $economyseat[$x];
if ($seatnumber == $seat)
{
unset($economyseat[$x]);
//$economyseat = array_remove_by_value($economyseat, $seat);
}
}
Assuming $seatnumber is another array, how about this...
$economyseat = array_diff($economyseat, $seatnumber);
Demo ~ https://eval.in/203332

PHP - Help in FOR LOOP

I have an array and looping through it and pushing the data using for loop.
$test_data = [10,20,35,01,50,60,87,12,45,86,9,85];
Actually that's a sample data. But my data is a result similar to that which I get from PostgreSQL, a single column data.
Using the below function for $test_data.
for( $x=0; $x < 12; $x++ ) {
$chart_data['data1'] = $test_data[$x];
}
Result : {"data1":{"data": 85"}}
Here's the for loop which I use along the PostgreSQL result in the PHP.
for( $x=0; $x < pg_num_rows($query); $x++ ) {
$data['data1'] = pg_fetch_assoc($query);
}
Even here I get only the last row in the browser when I do - echo json_encode($data);
Something similar to the result what I've mentioned above.
First 9 rows are not getting inserted into the data[]. Only the last row is getting added to the array.
Please say me where I'm doing the mistake.
Thanks in advance.
Since arrays cannot have same key as index.
You need to rewrite it as ..
for( $x=0; $x < 12; $x++ ) {
$chart_data['data1'][] = $test_data[$x];
// ^^ <--- Add that.
}
As Loic suggested go with a foreach , I answered quickly so it didn't strike on my mind in the first place
foreach($test_data as $val)
{
$chart_data['data1'][] = $val;
}
You could do like this (without using pg_num_rows):
// better initialize the result array
$data = array('data1' => array());
while ($row = pg_fetch_assoc($query)) {
// here is the point, add element to the array
$data['data1'][] = $row;
}

Return values of an array from behind (last element) php

say I have an array
$test_backwards=array("something1","something2","something3");
this is just a testing example and it's important to note that values will be added dinamically in my final array. so is it possible to dynamically return values from behind, namely starting from the last element?
something like this but backwards
for($i=0;$i<count($test_backwards);$i++) {
echo $test_backwards.'<br>';
}
Just start at the end and decrement your index:
for ($i = count($test_backwards) - 1; $i >= 0; $i--) {
echo $test_backwards[$i].'<br>';
}
or use array_reverse() (slower):
$test_backwards = array_reverse($test_backwards);
for ($i = 0; $i < count($test_backwards); $i++) {
echo $test_backwards[$i].'<br>';
}
You can also use array_pop(), if you do not need to keep this array. Or you can assign it to a temp array and then array_pop it, it will get and delete value from last.
$temp = $test_backwards;
while(($item = array_pop($temp)) !== NULL ) {
echo $item;
}

Get last number from a loop to reference it as an index in an array

I want to retrieve the last element in an array from $max and reference it in the array $set_of_path. I want only the last value. The problem with this code is this $max value who prints value 0 to end of file, thus instead of output the last value .. every element is implemented.. Can someone help me please. thanks in advance.
for($ind=0;$ind <count($array_file[0]['pair']);$ind++) {
....
...
if((($city)===$initial or ($city[0]===$initial))){
$x =$array_file[0]['pair'][$ind]['city'];//display all cities
if ((end($x))===$final){
//push $array_file content into the array
$xd[]=$array_file[0]['pair'][$ind];
$count=count(end($xd));
$last_element = array();
$a[] = array_merge($xd);
$end = count(end($a));
$push = array_push($last_element,$end);
$max = max($last_element)-1;
print_r($max);
$set_of_path=#$a[$max];
print_r($set_of_path);
print_r('<pre>');
}
};
...
...
.
...
.
..
Use SizeOf for getting the count of the items in an array.
http://php.net/manual/en/function.sizeof.php
Solution
<?php
$i = 0;
//Process array in loop
//increment $i++; or $i = sizeof($array);
?>

Categories