How to store string variables in php array - php

$matrix=array($_SESSION['review_buffer_name'],$_SESSION['review_buffer_mail'],$_SESSION['review_buffer_comment']);
The above line of code is inside WHILE loop So that it stores more than one record of array.Is it correct way to store records?. And how can we access each record and value of matrix?
$matrix should store multiple row of arrays... The problem is when i'm accessing $matrix[2] then it is giving second value of array... instead of second record of array

You can try it:
//Before while loop declare the array variable
$matrix = array();
While(your condition){
$matrix[] = array(
$_SESSION['review_buffer_name'],
$_SESSION['review_buffer_mail'],
$_SESSION['review_buffer_comment']
);
}
//To access array:
print_r($matrix[0]); //print_r whole first row. (array start from 0)
echo $matrix[0][0]; //echo single data that first row's first data
Or you can set index as name like:
//Before while loop declare the array variable
$matrix = array();
While(your condition){
$matrix[] = array(
'review_buffer_name'=>$_SESSION['review_buffer_name'],
'review_buffer_mail'=>$_SESSION['review_buffer_mail'],
'review_buffer_comment'=>$_SESSION['review_buffer_comment']
);
}
//Then access array:
print_r($matrix[0]); //print_r whole first row. (array start from 0)
echo $matrix[0]['review_buffer_name']; // first row's first data

Related

Loop through 5 dimensional array in PHP

I have a json response as array
$responseArray['business_discovery']['media']['data'][0]['id']
I'm trying to loop through the 'data' element and grab the value of 'id' and place that into an array like
$idarray[0] = $responseArray['business_discovery']['media']['data'][0]['id']
next iteration
$idarray[1] = $responseArray['business_discovery']['media']['data'][1]['id']
What is the best loop to accomplish this?
You should be able to set data to a variable and loop through that.
$data = $responseArray['business_discovery']['media']['data'];
foreach($data as $item) {
// do something with the id
$thisId = $item['id']
}

Is there a predefined function available to delete every element of an array in PHP?

I want to delete every element of an array in PHP using a predefined function
i have already tried the reset function which only set the internal pointer of an array to its first element and i have also done the task by using a for loop
$arr = [1,2,3,4,5];
for ($i=count($arr); $i > 0 ; $i--) {
array_pop($arr);
}
As already said in comments - deleting all elements in array means that you will have an empty array, so just:
$arr = [1,2,3,4,5];
$arr = [];
Also, unset($a); can be used, but after unset usage of $arr variable will throw undefined variable notice.
I think you want to empty the array indirectly:
<?php
$sports = array("Baseball", "Cricket", "Football", "Shooting");
// Deleting All Elements From array Sports
$sports = array(); //Just Set the empty array to your var.
print_r($sports);
?>
Above Example: http://phpfiddle.org/main/code/f6sj-zhki
For Your Example: http://phpfiddle.org/main/code/hu17-7bnk
I have got the simplest method of doing the task
$arr = []

Search multi-dimesional array and return specific value

Hard to phrase my question, but here goes. I've got a string like so: "13,4,3|65,1,1|27,3,2". The first value of each sub group (ex. 13,4,3) is an id from a row in a database table, and the other numbers are values I use to do other things.
Thanks to "Always Sunny" on here, I'm able to convert it to a multi-dimensional array using this code:
$data = '13,4,3|65,1,1|27,3,2';
$return_2d_array = array_map (
function ($_) {return explode (',', $_);},
explode ('|', $data)
);
I'm able to return any value using
echo $return_2d_array[1][0];
But what I need to be able to do now is search all the first values of the array and find a specific one and return one of the other value in i'ts group. For example, I need to find "27" as a first value, then output it's 2nd value in a variable (3).
You can loop through the dataset building an array that you can use to search:
$data = '13,4,3|65,1,1|27,3,2';
$data_explode = explode("|",$data); // make array with comma values
foreach($data_explode as $data_set){
$data_set_explode = explode(",",$data_set); // make an array for the comma values
$new_key = $data_set_explode[0]; // assign the key
unset($data_set_explode[0]); // now unset the key so it's not a value..
$remaining_vals = array_values($data_set_explode); // use array_values to reset the keys
$my_data[$new_key] = $remaining_vals; // the array!
}
if(isset($my_data[13])){ // if the array key exists
echo $my_data[13][0];
// echo $my_data[13][1];
// woohoo!
}
Here it is in action: http://sandbox.onlinephpfunctions.com/code/404ba5adfd63c39daae094f0b92e32ea0efbe85d
Run one more foreach loop like this:
$value_to_search = 27;
foreach($return_2d_array as $array){
if($array[0] == $value_to_search){
echo $array[1]; // will give 3
break;
}
}
Here's the live demo.

Array ( [0] => Array ... problems extracting array values

I am trying to run a function that gets information from a DB and returns an array of the values, so I can then extract it on the page.
Inside the function, after my query I have the following code:
$example_array = array();
while ($row = mysql_fetch_assoc($query) {
$example_array[] = $row;
}
return $example_array;
And there ends my function. Outside of it, I have this:
extract($example_array);
And I would assume I could then directly echo any of the variables that were previously in $example_array, e.g. <?= $example_var ?> but they do not contain any data.
Running print_r($example_array); gives an array that looks like this:
Array ( [0] => Array ( [example_var] => Example String ) )
The start of that code makes me think my array is somehow "lost" inside another array's first ([0]) value, and as such is not extracting correctly.
Have I gone about adding data to that initial $example_array incorrectly?
When you do $example_array[] = $row;, you assign the current row to a new index of $example_array. If you want to access it like $example_array['row_name'], you'd have to assign it like this:
$example_array = $row;
But when you do this, $example_array will be overwritten until it has reached the last row (which means that $example_array will always contain the last row from the query). If you just want the first row, you can do this and skip the whole while loop:
$example_array = mysql_fetch_assoc($query);
Maybe :
$example_array = array();
while ($row = mysql_fetch_assoc($query) {
array_push($example_array, $row['exemple_var']);
}
return $example_array;
The issue is that mysql_fetch_array would have meant $row['row_name'] was valid.
As you added $row to the array $example_array, you now need to access it via it's array id too, such as;
$example_array[0]['row_name'], $example_array[1]['row_name'] etc.
What exactly are you trying to achieve? May be easier to offer assistance if we know.

Storing array within an array using PHP

foreach ($topicarray as $key=>$value){
$files = mysql_query("mysqlquery");
while($file = mysql_fetch_array($files)){ extract($file);
$topicarray[$value] = array( array($id=>$title)
);
}
}
The first foreach loop is providing me with an array of unique values which forms a 1-dimensional array.
The while loop is intended to store another array of values inside the 1-dimensional array.
When the while loop returns to the beginning, it is overwriting it. So I only ever get the last returned set of values in the array.
My array ends up being a two dimensional array with only one value in each of the inner arrays.
Feels like I'm missing something very basic here - like a function or syntax which prevents the array from overwriting itself but instead, adds to the array.
Any ideas?
Step 1. Replace $topicarray[$value] with $topicarray[$value][]
Step 2. ???
Step 3. Profit
Make $topicarray[$value] an array of rows, instead of one row. Also, don't use extract here.
foreach ($topicarray as $key => $value) {
$rows = array();
$files = mysql_query("mysqlquery");
while($file = mysql_fetch_array($files)) {
$rows[] = array($file['id'] => $file['title']);
}
$topicarray[$value] = $rows;
}
Also, you should switch to PDO or MySQLi.

Categories