php How to access array inside array - php

I have an array which is of the following form in PHP-
Array(
0 =>
array (
0 => 'var1=\'some var1\'',
1 => 'var2=\'some_var2\'',
2 => 'var3=\'some_var3\'',
))
and I want it to appear as-
array (
0 => 'var1=\'some var1\'',
1 => 'var2=\'some_var2\'',
2 => 'var3=\'some_var3\'',
)
So how to do it?

Have you tried...
$inner_array = $outer_array[0];
var_dump($inner_array);
...?
Read here in the manual about more details to arrays in php.

Multidimensional arrays generally work as shown below
$shop = array( array("rose", 1.25 , 15),
array("daisy", 0.75 , 25),
array("orchid", 1.15 , 7)
);
echo $shop[0][0]." costs ".$shop[0][1]." and you get ".$shop[0][2];

The syntax of foreach construct is as follow:
foreach ($array_expression as $value) {
statement
}
foreach ($array_expression as $key => $value) {
statement
}
The first form loops over the array given by $array_expression. On each loop, the value of the current element is assigned to $value and the internal array pointer is advanced by one so that on the next loop, the next element is accessed.
The second form does the same thing, except that the current element’s key will be assigned to the variable $key on each loop.
The foreach syntax above is also similar to the following while construct:
while (list(, $value) = each($array_expression)) {
statement
}
while (list($key, $value) = each($array_expression)) {
statement
}
Even for loop can be used to process and loop through all elements of arrays with the following syntax:
$count = sizeof($arr_expression);
for ($i = 0; $i < $count; $i++) {
$value = $arr_express[$i];
statement
}
for ($i = 0, $item = ''; false != ($value = $arr_expression[$i]); $i++) {
statement
}
Nested loop of “foreach” can also be used to access multidimensional arrays. Here’s an example array and the way to access its value data. For example:
foreach ($array_expression as $arr_value) {
foreach ($array_value as $value) {
statement
}
}
Here’s an example code to access an array:
$contents = array(
"website_1" => array("name" => "StackOverFlow",
"url" => "http://www.stackoverflow.com",
"favorite" => "yes"),
"website_2" => array("name" => "Tip and Trick",
"url" => "http://www.tipandtrick.net",
"favorite" => "yes")
);
To retrieve the data, programmers can specify the keys that lead to the value directly with the following syntax, which will print the “My Digital Life”.
echo $contents['website_1']['name'];
However, the syntax above becomes unpractical when dealing with large arrays, or when the name of the keys and values changed dynamically. In this case, the “foreach” function can be used to access an array recursively.
foreach ($contests as $key => $list) {
echo "Website No.: " . $key . "\n";
echo "Name: " . $list['name'] . "\n";
echo "URL: " . $list['url'] . "\n";
}
The output will be:
Website No.: website_1
Name: StackOverFlow
URL: http://www.stackoverflow.com/
Website No.: website_2
Name: Tip and Trick
URL: http://www.tipandtrick.net/

Related

How to get array values data in PHP

I need small doubt in PHP. how to get below PHP array data in to variables.
$arraydata = Array
(
[366] => 1084569.5892969
[181TO365] => -2128157.619635
[121TO180] => -59235.780429687
[91TO120] => -266089.29
[61TO90] => -56390
[0TO60] => 8212872.9800098
)
This is my array output data.i need to get these data in to as variables like below.
$366data= 1084569.5892969;
$181TO365data = -2128157.619635;
$121TO180data = -59235.780429687;
$91TO120data = -266089.29;
$61TO90data = -56390;
$0TO60data = 8212872.9800098;
Like this variables get data . Can anyone help
Apart from the fact that PHP variables cannot start with a number (_ or letter) you can just loop over it like so:
<?php
foreach( $array as $key => $value ) {
$$key = $value;
}
The $$ before key means that you assign the value of $key to a variable with that name. But you probably to prefix it with an _ to make this work.
For example:
<?php
foreach( $array as $key => $value ) {
$prefixed = '_' . $key;
$$prefixed = $value;
}
Why do you want this by the way?

PHP dynamic array name with loop

I need to get values of array through a loop with dynamic vars.
I can't understand why the "echo" doesn’t display any result for "$TAB['b']".
Do you know why ?
The test with error message : https://3v4l.org/Fp3GT
$TAB_a = "aaaaa";
$TAB['b'] = "bbbbb";
$TAB['b']['c'] = "ccccc";
$TAB_paths = ["_a", "['b']", "['b']['c']"];
foreach ($TAB_paths as $key => $value) {
echo "\n\n\${'TAB'.$value} : "; print_r(${'TAB'.$value});
}
You are treating the array access characters as if they are part of the variable name. They are not.
So if you have an array $TAB = array('b' => 'something');, the variable name is $TAB. When you do ${'TAB'.$value}, you're looking for a variable that's actually named $TAB['b'], which you don't have.
Since you say that you just want to be able to access array indexes dynamically based on the values in another array, you just put the indexes alone (without the array access characters) in the other array.
$TAB['b'] = 'bbbbbb';
$TAB['c'] = 'cccccc';
$TAB_paths = array('b', 'c');
foreach ($TAB_paths as $key => $value) {
echo "\n\n".'$TAB['."'$value'".'] : ' . $TAB[$value];
}
Output:
$TAB['b'] : bbbbbb
$TAB['c'] : cccccc
DEMO
It's unclear what you're trying to do, although you need to include $TAB_all in $TAB_paths:
$TAB_paths = [$TAB_all['a'], $TAB_all['aside']['nav']];
Result:
${TAB_all.aaaaa} : ${TAB_all.bbbbb} :
Not certain what you're needing. My guess you need to merge two arrays into one. Easiest solution is to use the array_merge function.
$TAB_paths = array_merge($TAB_a1, $TAB_a2);
You can define the variable first
foreach ($TAB_all as $key => $value) {
${"TAB_all" . $key} = $value;
}
Now Explore the result:
foreach ($TAB_all as $key => $value) {
print_r(${"TAB_all" . $key});
}

PHP assoiative Array with almost Similar Keys

I have an associative array
array(
'item_name1' => 'PCC',
'item_name2' => 'ext',
'item_number1' => '060716113223-13555',
'item_number2' => '49101220160607-25222)',
)
What i Want to do is catch all the array keys where the key name has similarities
for example
i want to echo out item_name (it should get both item_name1 & item_name2) but i require it in a loop (foreach/for) so that i can send within the loop the details to my database for each set of values
Thanks For the help
Use the funtion array_keys
$allKeys = array_keys($yourArray);
$amountKeys = count($allKeys);
Unless you do not provide more code this will give you all the keys of $yourArray
Reference - array_keys
To get all the similar keys you can use this function similar-text()
Since I do not know how "similar" a key can be I would suggest you to test out different values and find a degree that matches your expectations.
Your data model seems to use numeric suffixes as a replacement for arrays so I'll assume that key name has similarities is an overestimated problem statement and you merely want to fix that.
The obvious tool is regular expressions:
$original_data = array(
'item_name1' => 'PCC',
'item_name2' => 'ext',
'item_number1' => '060716113223-13555',
'item_number2' => '49101220160607-25222)',
);
$redacted_data = array();
foreach ($original_data as $key => $row) {
if (preg_match('/^(.+)(\d+)$/u', $key, $matches)) {
$redacted_data[$matches[1]][$matches[2]] = $row;
} else {
$redacted_data[$key][] = $row;
}
}
var_dump($redacted_data);
It should be easy to tweak for your exact needs.
I can't figure out what the i require it in a loop (foreach/for) requirement means but you can loop the resulting array as any other array:
foreach ($redacted_data as $k => $v) {
foreach ($v as $kk => $vv) {
printf("(%s,%s) = %s\n", $k, $kk, $vv);
}
}
<?php
$items=[];
$itemsNumbers=[];
foreach($itemarr as $key=> $val)
{
$itempos = strpos("item_name", $key);
if ($pos !== false)
$items[]=$val;
$numberpos = strpos("item_number", $key);
if ($numberpos !== false)
$itemsNumbers[]=$val;
}
?>
Note: here $itemarr is your input array
$items you will get list of item names and $itemsNumbers you will get list of itemsNumbers

Match values in a multidemsional array

I would like to search an array for one value in a key and return the content from another key in the same array.
Like this:
$cars = array
(
array("brand" => "Volvo","color" => 22),
array("brand" => "BMW","color" => 15),
array("brand" => "Saab","color" => 5),
array("brand" => "Land Rover","color" => 17)
);
// Not working, just to clarify my intention
if($cars['brand'] == 'BMW') {echo $cars['color'];}
In this example, 15 should be echoed.
How can this be accomplished?
This should do it:
foreach($cars as $car) {
if($car['brand'] == 'BMW') {
echo $car['color'];
}
}
You need to create a loop that goes through each possible value. Often, you'd just use a for loop.
for($i = 0; $i < count($cars); $i++) {
if($cars[$i]['brand'] == "BMW")
echo $cars[$i]['color'];
}
However, using a foreach loop is also an option, and it looks neater.
foreach($cars as $v) {
if($v['brand'] == "BMW")
echo $v['color'];
}
See the array, for loop and foreach loop documentation for more information
Arrays
For loop
Foreach loop
You can use the array_filter function in PHP. See this page from the PHP docs and the given example.

Reading nested array?

I have an array that has a nested array shown below, my goal is to use PHP and loop through this array pulling out both the parent and child data.
$NestedArray = array(
"Name" => array(
"Phone"=>"Text",
"Email"=>"12"
)
"Company" => array(
"Phone"=>"Text",
"Email"=>"12"
)
);
Will someone please show me how to loop through this array using php. Thank you.
Use the foreach construct
foreach($NestedArray as $sKey=>$subarray)
{
// You can print the subarray key here too
foreach($subarray as $key=>$value)
{
print $key + ": " + $value;
}
}
if you want to print for debug purposes, use print_r
print_r($NestedArray);

Categories