extract string from object array - php

I have the following php:
$getlists = new CS_REST_Campaigns($_POST['campaign_id'], $auth);
$getlistsresult = $wrap->get_lists_and_segments();
if($getlistsresult->was_successful()) {
echo '<pre>';
var_dump($getlistsresult->response);
echo '</pre>';
}
The above outputs:
object(stdClass)#310 (2) {
["Lists"]=>
array(1) {
[0]=>
object(stdClass)#309 (2) {
["ListID"]=>
string(32) "xxxxxxxxxxxxxxxxxxxxxx"
["Name"]=>
string(6) "List 1"
}
}
["Segments"]=>
array(0) {
}
}
How would i target / extract the ListID?
I have tried the following which doesnt return anything:
foreach($getlistsresult->response->Results as $entry) {
echo $entry->ListID;
}

Looks like there's a mistake in your foreach:
foreach($getlistsresult->response->Lists as $entry) {
echo $entry->ListID;
}

You are writing Lists as Results.
foreach($getlistsresult->response->Lists as $entry) {
echo $entry->ListID;
}

directly by:
echo $getlistsresult->response->Lists[0]->ListID
or all:
foreach($getlistsresult->response->Lists as $entry) {
echo $entry->ListID;
}

Try type casting
$array = (array)$getlistsresult->response
$array = $array["Lists"]
foreach($array as $value)
{
echo $value['ListID']
}
or use object Iteration
$list = $getlistsresult->response->Lists
foreach($list as $key=>$value)
{
echo $value->ListId
}

Related

PHP arrray and string

I have problems with arrays. When I try to get the first array [0] it does not give me anything.
This is output
array(4) { [0]=> string(0) "" [333]=> string(123) "https://s3-us-west-2.amazonaws.com/hl-cdn-prod60/f/de/d6/fded6f1587f863a9e8fc1c2173143a8782fa655e/700Wx700H-105395-0416.jpg" [334]=> string(125) "https://s3-us-west-2.amazonaws.com/hl-cdn-prod60/e/b9/54/eb954216442547d2ed2c71adbcf73d4f2b3ef903/700Wx700H-105395-a-0416.jpg" [335]=> string(125) "https://s3-us-west-2.amazonaws.com/hl-cdn-prod60/7/16/95/71695917dd17d29648c8f4907000e3c6cab64581/700Wx700H-105395-b-0416.jpg" }
and this is code
private function getImages($dom) {
$images = [];
foreach ($dom->getElementsByTagName('ul') as $ul) {
if ($ul->getAttribute('class') == 'image-thumbnails') {
foreach ($dom->getElementsByTagName('li') as $li) {
$images[] = $li->getAttribute('data-zoom-url');
}
}
}
$images = array_unique($images);
return $images;
}
If you want to exclude empty values, check if the attribute is empty before adding it to the array:
if(!empty($li->getAttribute('data-zoom-url'))) {
$images[] = $li->getAttribute('data-zoom-url');
}

Identical key in array

I have an textfield where you can add comma separated values: Address1, Adress2
When you hit submit, information will be returned for each adress you typed in.
I want to treat the information as an array, and use the separated values that user user entered, as keys in the array.
I have the following code:
if(isset($_POST['submit']))
{
$addresses = explode(",", $_POST['addresses']);
$ort = $_POST['omrade'];
$obj = array();
foreach($addresses as $address)
{
echo $newSpider->fetchPage($address, $ort, $offst=0);
if(!isset($obj[$address]))
{
$obj[$address] = array();
echo "<pre>";
var_dump($address);
echo "</pre>";
}
}
}
When I type in two values, Adress1 and Adress2, the output of var_dump is:
array(1) {
["Address1"]=>
array(0) {
}
}
array(2) {
["Address1"]=>
array(0) {
}
["Address2"]=>
array(0) {
}
}
I want it to be like this:
array(1) {
["Address1"]=>
array(0) {
}
["Address2"]=>
array(0) {
}
}
What am I doing wrong? Anyone who can help me and explain?
Your code basically var_dumps for every loop. Just var_dump it after loop. Try this
if(isset($_POST['submit']))
{
$addresses = explode(",", $_POST['addresses']);
$ort = $_POST['omrade'];
$obj = array();
foreach($addresses as $address)
{
echo $newSpider->fetchPage($address, $ort, $offst=0);
if(!isset($obj[$address]))
{
$obj[$address] = array();
echo "<pre>";
echo "</pre>";
}
}
var_dump($address);
}

Accessing different array values from inside a multi-dimentional array using a foreach

I have created an array using a foreach loop and the result loops like this:
array(8) {
[0]=> array(3) {
["shortdesc"]=> object(SimpleXMLElement)#63 (1) {...}
["longdesc"]=> object(SimpleXMLElement)#58 (1) {...}
["price"]=> object(SimpleXMLElement)#64 (1) {...}
}
[1]=> array(3) {
["shortdesc"]=> object(SimpleXMLElement)#67 (1) {...}
["longdesc"]=> object(SimpleXMLElement)#62 (1) {...}
["price"]=> object(SimpleXMLElement)#68 (1) {...}
}
(...)
}
Using this array and a foreach below:
foreach ($optionsArray as $innerArray) {
foreach ($innerArray as $value) {
echo "<li>$value</li>";
}
}
I can echo out all of the content of the array, but I want to be able to assign the different array parts to variables.
So:
foreach ($optionsArray as $innerArray) {
foreach ($innerArray as $value) {
echo "<li>$value->price $value->longdesc</li>";
}
}
I know $value is not an object, but how can I do what I am looking for? Thanks.
No need to use innerArray foreach loop ,Just try given below code:
foreach ($optionsArray as $innerArray)
{
echo $innerArray['price']."<br>";
echo $innerArray['longdesc']."<br>";
}
try something like this
foreach ($optionsArray as $innerArray) {
$object = (object) $innerArray;
echo $object->price;
}
foreach ($optionsArray as $innerArray)
{
echo "<li>" . $innerArray['price'] . " " . $innerArray['longdesc'] . "</li>";
}

PHP - create dynamic multidimensional file tree array

I want to create a file tree, and for this purpose I need to convert an array of files and directories to a multidimensional file tree array. For example:
array
(
'file.txt',
'dir1/',
'dir1/dir2/',
'dir1/dir2/dir3/',
'dir1/file.txt',
)
to
array
(
'file.txt',
'dir1' =>
array
(
'dir2' =>
array
(
'dir3' =>
array(),
),
'file.txt',
)
)
I've tried several functions to accomplish this, but non of them worked. The problem I've encountered for example that there is no easy way to convert an array ('test','test','test'),'test' to $array['test']['test']['test'] = 'test'.
Here's a shorter recursive one:
function dir_tree($dir) {
$files = array_map('basename', glob("$dir/*"));
foreach($files as $file) {
if(is_dir("$dir/$file")) {
$return[$file] = dir_tree("$dir/$file");
} else {
$return[] = $file;
}
}
return $return;
}
Take a look to my post here.
The answer is: strtok will save you.
<?php
$input = [
'/RootFolder/Folder1/File1.doc',
'/RootFolder/Folder1/SubFolder1/File1.txt',
'/RootFolder/Folder1/SubFolder1/File2.txt',
'/RootFolder/Folder2/SubFolder1/File2.txt',
'/RootFolder/Folder2/SubFolder1/SubSubFolder1/File4.doc',
];
function parseInput($input) {
$result = array();
foreach ($input AS $path) {
$prev = &$result;
$s = strtok($path, '/');
while (($next = strtok('/')) !== false) {
if (!isset($prev[$s])) {
$prev[$s] = array();
}
$prev = &$prev[$s];
$s = $next;
}
$prev[] = $s;
unset($prev);
}
return $result;
}
var_dump(parseInput($input));
Output :
array(1) {
["RootFolder"]=>
array(2) {
["Folder1"]=>
array(2) {
[0]=>
string(9) "File1.doc"
["SubFolder1"]=>
array(2) {
[0]=>
string(9) "File1.txt"
[1]=>
string(9) "File2.txt"
}
}
["Folder2"]=>
array(1) {
["SubFolder1"]=>
array(2) {
[0]=>
string(9) "File2.txt"
["SubSubFolder1"]=>
array(1) {
[0]=>
string(9) "File4.doc"
}
}
}
}
}
I have PHP snippet for that:
<?php
function wps_glob($dir) {
foreach (glob($dir . '/*') as $f) {
if(is_dir($f)) {
$r[] = array(basename($f) => wps_glob($f));
}
else {
$r[] = basename($f);
}
}
return $r;
}
function wps_files($path) {
$wpsdir = Array(
'root' => $path,
'struktur' => wps_glob($path)
);
return $wpsdir;
}
?>
example usage here

PHP array argument by value, gets modified when looping the array items by reference

I have been having issues with an array being modified when passed by value to a function.
I have inspected the code and inside the function the array is looped getting the elements by reference.
I was surprised to see that after the loop the array items are marked as referenced. I don't know what this means, but must be the origin of my problem.
Let me put an example to see the point.
<?php
error_reporting(E_ALL);
ini_set('display_errors' , 1);
$a = array( array(0) );
echo '--1--';var_dump($a);
dummy($a);
echo '--4--';var_dump($a);
function dummy($arg) {
foreach($arg as &$item) {
$item[0] = 3;
}
dummy2($arg);
echo '--3--';var_dump($arg);
}
function dummy2($arg) {
foreach($arg as &$item) {
$item[1]=9;
}
echo '--2--';var_dump($arg);
}
?>
After this code I would expect that in point 3, $arg would have only one element, but it has two, so it has been modified by dummy2 function.
The output is as follows:
--1--array(1) { [0]=> array(1) { [0]=> int(0) } }
--2--array(1) { [0]=> &array(2) { [0]=> int(3) [1]=> int(9) } }
--3--array(1) { [0]=> &array(2) { [0]=> int(3) [1]=> int(9) } }
--4--array(1) { [0]=> array(1) { [0]=> int(0) } }
Why are the arrays marked as &array after being looped by reference?
How can this be avoid?
You need to unset the loop variable that captures by reference:
<?php
error_reporting(E_ALL);
ini_set('display_errors' , 1);
$a = array( array(0) );
echo '--1--';var_dump($a);
dummy($a);
echo '--4--';var_dump($a);
function dummy($arg) {
foreach($arg as &$item) {
$item[0] = 3;
}
unset($item);
dummy2($arg);
echo '--3--';var_dump($arg);
}
function dummy2($arg) {
foreach($arg as &$item) {
$item[1]=9;
}
unset($item);
echo '--2--';var_dump($arg);
}
?>
See in the documentation for foreach, there is a big red warning that says:
Reference of a $value and the last array element remain even after the
foreach loop. It is recommended to destroy it by unset().
Use key => value pairs and return the array in your functions
<?php
error_reporting(E_ALL);
ini_set('display_errors' , 1);
$a = array( array(0) );
echo '--1--';var_dump($a);
$a = dummy($a);
echo '--4--';var_dump($a);
function dummy($arg) {
foreach($arg as $key => $value) {
$arg[$key][0] = 3;
}
return dummy2($arg);
}
function dummy2($arg) {
foreach($arg as $key => $value) {
$arg[$key][1]=9;
}
return $arg;
}
?>
http://codepad.org/f30c6FUj

Categories