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);
}
Related
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');
}
$update is a two dimensional associative array. Part of the var_dump is:
array(101) {
[0]=> array(27) { ["code"]=> string(4) "2014" ["na1"]=> string(4) "6010" and many more fields following }
[1]=> array(27) { ["code"]=> string(4) "2015" ["na1"]=> string(4) "6010" and many more fields following }
and many more subarrays following of course . . .
Need to replace the code value with a name and created:
foreach($update as $key=>$subarray){;
foreach ($subarray as $subkey=>$val) {
echo $subkey.$val."<br>";//Just for checking
if ($subkey=='code' && $val==2014)
{
$val="Name1";
}
elseif ($key=='code' && $val==2015)
{
$val="Name2";
}
}
}
var_dump($update);
The echo $subkey and $val give perfectly the correct values, however de If statement seems never to be true (or is cancelled out again somehow) as the var_dump is leading again to the original values
Some Stackoverflow research even showed constructions with only one foreach loop, much more elegant, but seems not to reach the second array level.
Is there a better approach? Solution to fix this one?
You are not updating the source array.
foreach($update as $key=>$subarray){
foreach ($subarray as $subkey=>$val) {
echo $subkey.$val."<br>";//Just for checking
if ($subkey=='code' && $val==2014)
{
//$val="Name1";
$update[$key][$subkey] = "Name1" ;
}
elseif ($key=='code' && $val==2015)
{
//$val="Name2";
$update[$key][$subkey] = "Name2" ;
}
}
}
You can access to the field you want by index.
Example :
$i = 0;
$j = 0;
foreach($update as $key=>$subarray){;
foreach ($subarray as $subkey=>$val) {
echo $subkey.$val."<br>";//Just for checking
if ($subkey=='code' && $val==2014)
{
$update[$i][$j] = "Name1";
}
elseif ($key=='code' && $val==2015)
{
$update[$i][$j] = "Name2";
}
$j++
}
$j = 0;
$i++;
}
var_dump($update);
$newArr = array();
foreach($update as $key=>$subarray){
$subNewArr = array();
foreach ($subarray as $item) {
if ($item['code']==2014)
{
$item['code']="Name1";
}
elseif ($item['code']==2015)
{
$item['code']="Name2";
}
array_push($subNewArr, $item);
}
array_push($newArr, $subNewArr);
}
var_dump($update);
Less if else checking
I cannot get any output from my multi-dimensional array. I'm trying to get my data using the push_array method.
I would like to get my output as:
John, 1001
Tom, 1002
Jerry, 1003
Sarah, 1004
However, I'm not getting any output. I know the array is working though as I get the following output when I echo $message and do not use $id and $name in my output foreach array.
Current output: (when echoing $message)
1
2
3
4
But nothing gets shown when I use $id and $name as shown here.
<ul class="result"><?php
foreach ($output as $message) {
$id = $message[1][0];
$name = $message[1][1];
?><li>
<?php echo $name; ?>
</li><php } ?>
</ul>
Here is my full code:
<?php
$Download = new Download();
$result = array(); //store results of upload
try {
$Download->showDBFiles();
$output = $Download->getMessages();
} catch (Exception $e) {
$result[] = $e->getMessage();
}
?>
<h2>Output</h2>
<?php if ($output) { ?>
<ul class="result">
<?php foreach ($output as $message) {
$id = $message[1][0];
$name = $message[1][1];
?>
<li><?php echo $name; ?></li>
<?php } ?>
</ul>
<?php } ?>
Function where arrays are defined
protected $messages = array();
public function showDBFiles() {
global $database;
$values=array();
$sql = "SELECT resume_id, resume_title FROM ".self::$table_name;
$result = $database->query($sql);
if(mysqli_num_rows($result)==0){
$this->messages[] = "Database is empty";
}else{
while(list($id, $name) = mysqli_fetch_array($result)){
array_push($this->messages, array($id, $name));
}
}
}
public function getMessages()
{
return $this->messages;
}
UPDATE:
After using the suggestion by jedrzej.kurylo I'm able to have the $id and $name values as shown in my var_dump output:
array(4) { [0]=> array(2) { [0]=> string(2) "73" [1]=> string(2) "qd" } [1]=> array(2) { [0]=> string(2) "72" [1]=> string(3) "jav" } [2]=> array(2) { [0]=> string(2) "70" [1]=> string(4) "da12" } [3]=> array(2) { [0]=> string(2) "71" [1]=> string(3) "jav" } }
However for some reason that I'm unsure of my output is now this:
d
a
a
a
The problem is with how you use array_push here:
$this->messages[] = array_push($values, array($id, $name));
array_push does not return updated array but the new number of items in the array - see http://php.net/manual/en/function.array-push.php. That's why you're getting increasing integers in your output.
Do the following instead:
while(list($id, $name) = mysqli_fetch_array($result)){
array_push($this->messages, array($id, $name));
}
or just
while(list($id, $name) = mysqli_fetch_array($result)){
$this->messages[] = array('id'=>$id, 'name'=>$name);
}
Then you can access $id and $name like this:
$id = $message['id'];
$name = $message['name'];
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
}
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