php read object of array in array - php

$values = (object)$arr;
var_dump($values);
var_dump produces these:
object(stdClass)[1]
public 'time' => float 0.002
public 'distance' => float 0.156
public 'code' => string '1' (length=1)
public 'result' =>
array (size=17)
0 => string '2.94053, 101.787, A' (length=24)
1 => string '2.94043, 101.787, A' (length=24)
2 => string '2.9404, 101.787, A' (length=23)
3 => string '2.94029, 101.787, A' (length=24)
4 => string '2.94025, 101.787, A' (length=24)
5 => string '2.9402, 101.787, A' (length=23)
6 => string '2.94016, 101.787, A' (length=24)
7 => string '2.94007, 101.787, A' (length=24)
public 'arrayPosition' =>
array (size=1)
0 =>
array (size=1720)
0 => string '2.93955, 101.788, B' (length=22)
1 => string '2.93951, 101.788, B' (length=22)
2 => string '2.93926, 101.788, B' (length=22)
3 => string '2.93921, 101.788, B' (length=22)
4 => string '2.9392, 101.788, B' (length=21)
5 => string '2.93911, 101.788, B' (length=22)
6 => string '2.93906, 101.789, B' (length=22)
7 => string '2.93896, 101.789, B' (length=22)
How can I read each of the values of result and arrayPosition ?
By using this
echo "<br>".$values->time."<br>";
echo $values->distance."<br>";
echo $values->code."<br>";
echo $values->result."<br>";
echo $values->arrayPosition."<br>";
will fail on result and arrayPosition
Notice: Array to string conversion

to access arrayPosition there are couple of ways if you know the sub index of the element you want you can access it by
$values->arrayPosition[0][index_number]; //you may require the first dimension index if you have more than one element there as well
The other method is using a loop
foreach ($values->arrayPosition as $levelone) {
foreach ($levelone as $key => $leveltwo) {
echo $leveltwo;
}
}

foreach($values as $value)
{
foreach($value->result as $result1)
{
echo($result1);
echo("<br>");
}
foreach($value->arrayPosition as $arrayposition2)
{
foreach($arrayposition2 as $arrayposition_child)
{
echo($arrayposition_child);
echo("<br>");
}
}
}

for accessing those 2 variables, you can:
1)
foreach( $values->result as $row ) {
echo $row;
}
2)
foreach( $values->arrayPosition as $mulRow ) {
foreach( $mulRow as $row ) {
echo $row;
}
}

Related

Sorting 'key=>value' pair multi-dimensional arrays in PHP

I am creating a site and I am trying to do a few complicated things with arrays to get it to work.
It is for an e-commerce site and each product on my site can have a number of attributes. I am trying to get each combination of attributes to do the pricing for them seperately.
First I get an array of the attribute ids ($attribute_id_array) and the query the database for the options for that array.
So if one attribute was colors the options here would be red,green,blue,etc,. or size would be small,medium,large,etc,. These are then stored in a new array ($attribute_arrays).
I then go through these to get every combination of attributes the product can have and sort these into a new array again ($new_attributes_array).
I then loop through this and create a price form for each combination.
$attribute_arrays = [];
foreach($attribute_id_array as $attribute_id){
$params = [$attribute_id];
$sql = "SELECT * FROM attributes WHERE id=?";
$attributeResult = DB::run($sql,$params);
while($row = $attributeResult->fetch(PDO::FETCH_ASSOC)){
array_push($attribute_arrays,$row);
}
}
var_dump($attribute_arrays);
function combinations($arrays, $i = 0) {
if (!isset($arrays[$i])) {
return array();
}
if ($i == count($arrays) - 1) {
return $arrays[$i];
}
$tmp = combinations($arrays, $i + 1);
$result = array();
foreach ($arrays[$i] as $v) {
foreach ($tmp as $t) {
$result[] = is_array($t) ?
array_merge(array($v), $t) :
array($v, $t);
}
}
return $result;
}
$new_attributes_array = combinations($attribute_arrays);
var_dump($new_attributes_array);
This is all working fine except I want to be able to get the keys for all of the key value pairs so I can reference it back to my database.
The way it comes out at the moment is like this:
$attribute_id_array:
array (size=2)
1 => string '5' (length=1)
2 => string '7' (length=1)
$attribute_arrays:
0 =>
array (size=2)
'attribute1' => string 'Step Through Bars' (length=17)
'attribute2' => string 'Gated' (length=5)
1 =>
array (size=2)
'attribute1' => string '3 metres' (length=8)
'attribute2' => string '6 metres' (length=8)
$new_attributes_array:
0 =>
array (size=2)
0 => string 'Step Through Bars' (length=17)
1 => string '3 metres' (length=8)
1 =>
array (size=2)
0 => string 'Step Through Bars' (length=17)
1 => string '6 metres' (length=8)
2 =>
array (size=2)
0 => string 'Gated' (length=5)
1 => string '3 metres' (length=8)
3 =>
array (size=2)
0 => string 'Gated' (length=5)
1 => string '6 metres' (length=8)
Is there a way to get it so that the key will be similar in format to:
0 =>
array (size=2)
5-attribute1 => string 'Step Through Bars' (length=17)
7-attribute1 => string '3 metres' (length=8)
1 =>
array (size=2)
5-attribute1 => string 'Step Through Bars' (length=17)
7-attribute2 => string '6 metres' (length=8)
Edit
So I changed the line array_push($attribute_arrays,$row); to $attribute_arrays[$attribute_id] = $row;.
This now means that $attribute_arrays now has the$attribute_id variable as the key like so:
array (size=2)
5 =>
array (size=2)
'attribute1' => string 'Step Through Bars' (length=17)
'attribute2' => string 'Gated' (length=5)
7 =>
array (size=2)
'attribute1' => string '3 metres' (length=8)
'attribute2' => string '6 metres' (length=8)
This now means my other function for getting the combinations won't work as it is using the $i variable as the index for the array starting at '0'.
Found another function online to sort it here How to generate in PHP all combinations of items in multiple arrays:
function combinations($arrays) {
$result = array(array());
foreach ($arrays as $property => $property_values) {
$tmp = array();
foreach ($result as $result_item) {
foreach ($property_values as $property_key => $property_value) {
$tmp[] = $result_item + array($property_key => $property_value);
}
}
$result = $tmp;
}
return $result;
}
However, this doesn't do exactly as I want and I end up with this:
array (size=4)
0 =>
array (size=1)
'attribute1' => string 'Step Through Bars' (length=17)
1 =>
array (size=2)
'attribute1' => string 'Step Through Bars' (length=17)
'attribute2' => string '6 metres' (length=8)
2 =>
array (size=2)
'attribute2' => string 'Gated' (length=5)
'attribute1' => string '3 metres' (length=8)
3 =>
array (size=1)
'attribute2' => string 'Gated' (length=5)
try this as your combinations function
modified code taken from here
function combinations($arrays) {
$result = array(array());
foreach ($arrays as $key => $values) {
$tmp = array();
foreach ($result as $item) {
foreach ($values as $k=>$value) {
$tmp[] = array_merge($item, array($key.'-'.$k => $value));
}
}
$result = $tmp;
}
return $result;
}
Instead of using while on following line
while($row = $attributeResult->fetch(PDO::FETCH_ASSOC)){
Use foreach loop to get key paired value. e.g
foreach($databaseFetchedRecord as $mykey=>$myvalue)

Comparing two object arrays with array_udiff

Array 1: $tags_result
array (size=4)
0 =>
object(stdClass)[8]
public 'id_tag' => string '2' (length=1)
public 'tag' => string 'tag 1' (length=5)
1 =>
object(stdClass)[9]
public 'id_tag' => string '5' (length=1)
public 'tag' => string 'tag 4' (length=5)
2 =>
object(stdClass)[10]
public 'id_tag' => string '6' (length=1)
public 'tag' => string 'tag 7' (length=5)
3 =>
object(stdClass)[11]
public 'id_tag' => string '7' (length=1)
public 'tag' => string 'tag 9' (length=5)
Array 2: $post_tags_result
array (size=2)
0 =>
object(stdClass)[5]
public 'id_tag' => string '2' (length=1)
public 'tag' => string 'tag 1' (length=5)
1 =>
object(stdClass)[6]
public 'id_tag' => string '6' (length=1)
public 'tag' => string 'tag 7' (length=5)
I'm trying to extract the values of the array 1 that do not appear in the array 2:
function foo($tags_result, $post_tags_result){
return $tags_result->id_tag != $post_tags_result->id_tag;
}
$difference_tags = array_udiff($tags_result, $post_tags_result, 'foo');
But the result return a common value: tag 1. I expect just tag 4 and tag 9.
array (size=3)
0 =>
object(stdClass)[8]
public 'id_tag' => string '2' (length=1)
public 'tag' => string 'tag 1' (length=5)
1 =>
object(stdClass)[9]
public 'id_tag' => string '5' (length=1)
public 'tag' => string 'tag 4' (length=5)
3 =>
object(stdClass)[11]
public 'id_tag' => string '7' (length=1)
public 'tag' => string 'tag 9' (length=5)
As an alternative, you could gather all the tags that needed to be excluded first. Then after that you could now filter it thru array_filter and get the desired result. Rough example:
$tags = array();
foreach($post_tags_result as $t) {
$tags[] = $t->tag; // gather all tags
}
// filter array using gathered tags
$result = array_filter($tags_result, function($v) use($tags){
return !in_array($v->tag, $tags);
});
Sample Output
From the manual:
The comparison function must return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second.
So, instead of a Boolean comparison you should do a diff to delete equal items. Since you are comparing numeric values, you should do something like:
$ php -a
php > $a = [['id'=>1],['id'=>2],['id'=>3],['id'=>4]];
php > $b = [['id'=>1],['id'=>4]];
php > $c = array_udiff($a, $b, function($a, $b){
return $a['id'] - $b['id'];
});
php > print_r($c);
Array
(
[1] => Array
(
[id] => 2
)
[2] => Array
(
[id] => 3
)
)
In your case:
$difference_tags = array_udiff($tags_result, $post_tags_result, function(($a1, $a2){
return $a1->id_tag - $q2->id_tag;
}));

Create multidimensional array from txt file

txt file that looks like this:
first item1:second item1:third item1:fourth item1:fifth item1
first item2:second item2:third item2:fourth item2:fifth item2
first item3:second item3:third item3:fourth item3:fifth item3
I want to be able to output each item sperately.
As far as I see it, I need to:
open the file with file()
create a multidimensional array, by first splitting up the lines
use explode() to split it up by :
I have tried various things but I can't get it to work. Anyone knows how to do that?
So you need something like this?
$lines = file('try.txt');
$output = array();
foreach ($lines as $line) {
$output[] = array_map(function($var) {
return rtrim($var, "\r\n");
}, explode(":", $line));
}
var_dump($output);
Output
array
0 =>
array
0 => string 'first item1' (length=11)
1 => string 'second item1' (length=12)
2 => string 'third item1' (length=11)
3 => string 'fourth item1' (length=12)
4 => string 'fifth item1' (length=11)
1 =>
array
0 => string 'first item2' (length=11)
1 => string 'second item2' (length=12)
2 => string 'third item2' (length=11)
3 => string 'fourth item2' (length=12)
4 => string 'fifth item2' (length=11)
2 =>
array
0 => string 'first item3' (length=11)
1 => string 'second item3' (length=12)
2 => string 'third item3' (length=11)
3 => string 'fourth item3' (length=12)
4 => string 'fifth item3' (length=11)

php how i can change my label in loop

I have an array in loop while like this when I var_dump it.
array (size=73)
0 => string '1' (length=1)
'address' => string '1' (length=1)
1 => string '2' (length=2)
'street_no' => string '2' (length=1)
array (size=73)
0 => string 'vbfgh' (length=5)
'address' => string 'vbfgh' (length=5)
1 => string 'fgfd' (length=4)
'street_no' => string 'fgfd' (length=4)
array (size=73)
0 => string 'vbfgh' (length=5)
'address' => string 'vbfgh' (length=5)
1 => string 'fgfd' (length=4)
'street_no' => string 'fgfd' (length=4)
array (size=73)
0 => string 'vbfgh' (length=5)
'address' => string 'vbfgh' (length=5)
1 => string 'fgfd' (length=4)
'street_no' => string 'fgfd' (length=4)
I want disply data on screen for first data is
`AddressMain: 1
streetMain: 1
.....
AddressLeft: vbfgh
street1Left: fgfd
.....
AddressRight: vbfgh
streetRight: fgfd
.....
AddressCenter: vbfgh
streetCenter: fgfd
.....`
How I can change my label like this if this code in loop while ?
And this is my code
while( $row = pg_fetch_array($result)){
echo "AddressMain:".$row['address'];
echo "streetMain:".$row['street_no'];
echo "<br/>";
echo ".......";
}
Plz help me how I can change my label in this loop ?
thanks
Use an array to define labels, then loop around it.
$labels = array('Main','Left','Right','Center');
$i = 0;
while( $row = pg_fetch_array($result)){
echo "Address".$labels[$i].":".$row['address'];
echo "street".$labels[$i].":".$row['street'];
echo "<br/>";
$i++;
}

get parent array name after array_walk_recursive function

I use the following function to verify if a search word is in the name of files of my folder.
$files2=list_files("documents/minelli");
Class Commentaire_filter{
static function test_print($item, $key, $value)
{
if (preg_match("#".$value."#", $item))
{
$array = Array($key=>$item);
print_r($array);
?>
<?php echo $key.' '. $item; ?><br />
<?php
}
}
}
array_walk_recursive($files2, 'Commentaire_filter::test_print',$motrecherche );
I obtain a list of files.
I would like add a link to permit users to download the file.
When i'm using the array_walk_recursive function, i can only get the name of file and the key. How can i get the name of parent arrays to made the link ?
Here an extract of my $files:
array (size=5)
'Administratifs' =>
array (size=5)
0 => string 'campagne-sanmarina.jpg' (length=22)
1 => string 'COSMO Echantillons ETE 2009.xls' (length=31)
2 => string 'COSMOPARIS Echantillons MARO Hiver 2011 311-411.xls' (length=51)
3 => string 'cosmoparis-boutique.jpg' (length=23)
4 => string 'minelli-20-ans.png' (length=18)
'Commerce' =>
array (size=4)
0 => string 'a-gagner-cosmoparis.jpg' (length=23)
1 => string 'CONTROLE 2009.pdf' (length=17)
2 => string 'cosmoparis-boutique.jpg' (length=23)
3 => string 'soldes-cosmoparis.jpg' (length=21)
'Gestion' =>
array (size=1)
'PROCEDURES' =>
array (size=5)
0 => string 'cosmoparis-boutique.jpg' (length=23)
1 => string 'flux-ecommerce-smc.pdf' (length=22)
2 => string 'Minelli-Lyon.jpg' (length=16)
3 => string 'sanmarina-magasin-interieur.jpg' (length=31)
4 => string 'visuel_chaussures_minelli_printemps_ete_2009.jpg' (length=48)
'Magasins' =>
array (size=2)
0 => string 'COSMO Echantillons ETE 2009.xls' (length=31)
1 => string 'san-marina-saint-etienne.jpg' (length=28)
'Ressources Humaines' =>
array (size=7)
'ACTUALITES PAYE' =>
array (size=3)
0 => string 'COSMOPARIS Echantillons MARO Hiver 2011 311-411.xls' (length=51)
1 => string 'cosmoparis-boutique25.jpg' (length=23)
2 => string 'minelli-tours.jpg' (length=17)
...
For 'cosmoparis-boutique25.jpg', i would like to get the parent array name ('Ressources Humaines'=>'ACTUALITES PAYE'). How can i get this information to build a link like 'myfolder/Ressources Humaines/ACTUALITES PAYE/cosmoparis-boutique25.jpg ?
Thank you for your help!
You will have to ditch array_walk_recursive and roll your own recursive walk. This will allow you to maintain or pass custom state information whenever you recurse.
For example:
function my_walk_recursive(array $array, $path = null) {
foreach ($array as $k => $v) {
if (!is_array($v)) {
// leaf node (file) -- print link
$fullpath = $path.$v;
// now do whatever you want with $fullpath, e.g.:
echo "Link to $fullpath\n";
}
else {
// directory node -- recurse
my_walk_recursive($v, $path.'/'.$k);
}
}
}

Categories