How to get first value of array php - php

How can i get the first value of this array?
I want get jpeg value from each array, but it just returns "hh".
This is my code:
$image_output = wp_get_attachment_image_src( $id );
var_dump($image_output);
foreach ($image_output as $value) {
echo $value;
}
This is my array
array(4) {
[0]=>
string(98) "localhost/wp-content/uploads/2018/06/11.jpeg"
[1]=>
int(170)
[2]=>
int(120)
[3]=>
bool(true)
}
array(4) {
[0]=>
string(63) "localhost/wp-content/uploads/2018/06/10.jpeg"
[1]=>
int(170)
[2]=>
int(120)
[3]=>
bool(true)
}
Where is my code wrong?

If I understand it correct it's a multidimensional array with the link in item 0 of each array.
Echo implode("<br>\n", array_column($image_output, 0));
This should output the links one on each line without looping

Related

unset array items at once which has same words and send those to another array

I'm trying to unset a couple items from an array at once, send unsetted items to another array.
array(6) {
[0]=> string(65) "https://www.kintetsu-re.co.jp/mansion_kansai/outline/midosuji241/"
[1]=> string(41) "https://geo.8984.jp/outline/suminodo.html"
[2]=> string(56) "http://www.sohgoh-outline.jp/index.php?bunjo_number=0141"
[3]=> string(56) "http://www.sohgoh-outline.jp/index.php?bunjo_number=0136"
[4]=> string(56) "http://www.sohgoh-outline.jp/index.php?bunjo_number=0099"
[5]=> string(53) "https://www.sgr-sumai.jp/mansion/tezukayama21/outline"
}
Three links in this array starts with http://www.sohgoh-outline.jp . So I'm trying to unset those. But not one by one. I can already doing it. For example, I tried to locate those with strpos
$needle = "http://www.sohgoh-outline.jp/";
foreach ($link as $unset){
if (($index = strpos($unset, $needle)) !== false){
$renai [] = $unset[$index];
unset($unset[$index]);
}
}
But this error popping up.
Cannot unset string offsets
Any suggestions?
change your code like this:
foreach ($link as $k => $unset){
if ((strpos($unset, $needle)) !== false)
{
$renai [] = $link[$k]; // This will add the value to new array.
unset($link[$k]); // THIS WILL UNSET THE VALUE.
}
}
The simplest method is probably regex with preg_grep and array_diff.
$out = preg_grep("/.*?(sohgoh-outline\.jp).*/", $arr);
var_dump($out); //sohgoh-outline.jp links
$arr = array_diff($arr, $out);
var_dump($arr); // all but sohgoh-outline.jp links
Output of above code:
array(3) {
[2]=>
string(56) "http://www.sohgoh-outline.jp/index.php?bunjo_number=0141"
[3]=>
string(56) "http://www.sohgoh-outline.jp/index.php?bunjo_number=0136"
[4]=>
string(56) "http://www.sohgoh-outline.jp/index.php?bunjo_number=0099"
}
array(3) {
[0]=>
string(65) "https://www.kintetsu-re.co.jp/mansion_kansai/outline/midosuji241/"
[1]=>
string(41) "https://geo.8984.jp/outline/suminodo.html"
[5]=>
string(53) "https://www.sgr-sumai.jp/mansion/tezukayama21/outline"
}
https://3v4l.org/Um46H

PHP Multudimensional Array foreach

Using PHP and MySQL, I have generated an array called $response.
A var_dump of $response can be seen here.
array(2) {
["OperationRequest"]=>
array(4) {
["HTTPHeaders"]=>
array(1) {
[0]=>
array(1) {
["#attributes"]=>
array(2) {
["Name"]=>
string(9) "UserAgent"
["Value"]=>
string(14) "ApaiIO [2.1.0]"
}
}
}
["RequestId"]=>
string(36) "f53f381e-efb3-4fef-8e39-4f732b4b463e"
["Arguments"]=>
array(1) {
["Argument"]=>
array(11) {
[0]=>
array(1) {
["#attributes"]=>
array(2) {
["Name"]=>
string(14) "AWSAccessKeyId"
["Value"]=>
string(20) "KEY"
}
}
[1]=>
array(1) {
["#attributes"]=>
array(2) {
["Name"]=>
string(12) "AssociateTag"
["Value"]=>
string(11) "TAG"
}
}
[2]=>
array(1) {
["#attributes"]=>
array(2) {
["Name"]=>
string(6) "IdType"
["Value"]=>
string(4) "ISBN"
}
}
[3]=>
array(1) {
["#attributes"]=>
array(2) {
["Name"]=>
string(6) "ItemId"
["Value"]=>
string(38) "0751538310,9780141382067,9781305341141"
}
}
[4]=>
array(1) {
["#attributes"]=>
array(2) {
["Name"]=>
string(9) "Operation"
["Value"]=>
string(10) "ItemLookup"
}
}.......so on
A json_encode of the array can be seen here (as requested in a comment).
I'd like to select the Title from these two items. From what I can see this is located at;
Items > Item > ItemAttributes > Author
So, using a foreach loop I have tried the following;
foreach ($response as $item) {
echo $item['Items']['Item']['ItemAttributes']['Title']; // line 2
}
However this returns the following error;
Message: Undefined index: Items. Line Number: 2
Where am I going wrong and what must I change in my code in order to achieve the desired result?
Also, any advice on how to 'read' multidimensional arrays would be greatly appreciated.
Thanks
Try this one, it will help you out. You were are iterating on the wrong key that's why you were not getting desired output.
Try this code snippet herefrom json provide by OP in question
foreach($array["Items"]["Item"] as $key => $value)
{
print_r($value["ItemAttributes"]["Title"]);
echo PHP_EOL;
}
Output:
Panic
Panic
Captain Flinn and the Pirate Dinosaurs: Missing Treasure! (Captain Flinn)
For getting unique titles:
foreach(json_decode($json,true)["Items"]["Item"] as $key => $value)
{
$result[]=$value["ItemAttributes"]["Title"];
echo PHP_EOL;
}
print_r(array_unique($result));
#Also, any advice on how to 'read' multidimensional arrays would be greatly appreciated.
Post your encoded json string to
http://json.parser.online.fr
"+" and "-" button at the right panel should help you read it easily.
//Check Items is not empty
if( !isset($response["Items"]["Item"]) || empty($response["Items"]["Item"]) )
throw New Exception("Empty Item");
foreach($response["Items"]["Item"] as $item){
$title = $item['ItemAttributes']['Title']
}
You should debug as:
foreach ($response as $key => $item) {
if(isset($item['Items'])){ //Check Items index defined
echo $item['Items']['Item']['ItemAttributes']['Title'];
}else{
var_dump($key);
}
}

Flatten multidimensional array not working

I have two arrays like the following.
$alerts_array=array(1) {
[0]=> array(11) {
["CustomAlertsID"]=> int(3)
["CustomAlerts_Name"]=> string(10) "title demo"
["CustomAlerts_PublishDate"]=> string(10) "2016-07-03"
["CustomAlerts_ExpiryDate"]=> string(10) "2016-07-21"
}
}
$singlebtn_array =array(3) {
["button_text0"]=> string(16) "Button Name1only"
["button_text1"]=> string(12) "button name2"
["button_text2"]=> string(16) "button name3_new"
}
I have merged the two arrays into a single multidimensional array which looks like following
$alerts_array = array_merge($alerts_array,$singlebtn_array);
array(4) { [0]=> array(11)
{ ["CustomAlertsID"]=> int(3)
["CustomAlerts_Name"]=> string(10) "title demo"
["CustomAlerts_PublishDate"]=> string(10) "2016-07-03"
["CustomAlerts_ExpiryDate"]=> string(10) "2016-07-21"
}
[1]=> array(1)
{ ["button_text0"]=> string(16) "Button Name1only" }
[2]=> array(1)
{ ["button_text1"]=> string(12) "button name2" }
[3]=> array(1) { ["button_text2"]=> string(16) "button name3_new" } }
I need both keys and values in the new flattened array
I need it like this:
array(4) { [0]=> array(11)
{ ["CustomAlertsID"]=> int(3)
["CustomAlerts_Name"]=> string(10) "title demo"
["CustomAlerts_PublishDate"]=> string(10) "2016-07-03"
["CustomAlerts_ExpiryDate"]=> string(10) "2016-07-21"
["button_text0"]=> string(16) "Button Name1only"
["button_text1"]=> string(12) "button name2"
["button_text2"]=> string(16) "button name3_new" }}
I have usedthe following code for combining.
$newArr = array();
foreach ($alerts_array as $key=>$tmp) {
$newArr = array_merge($newArr, array_values($tmp));
}
The $newArr is giving me the result ,but keys are lost
Simply merge the first key [0] of your $alerts_array, like this:
$alerts_array = array_merge($alerts_array[0], $singlebtn_array);
That will output:
array(7) {
["CustomAlertsID"]=>
int(3)
["CustomAlerts_Name"]=>
string(10) "title demo"
["CustomAlerts_PublishDate"]=>
string(10) "2016-07-03"
["CustomAlerts_ExpiryDate"]=>
string(10) "2016-07-21"
["button_text0"]=>
string(16) "Button Name1only"
["button_text1"]=>
string(12) "button name2"
["button_text2"]=>
string(16) "button name3_new"
}
Also see the working demo here.
To get what you want because alerts is an array of arrays:
$alerts_array = array_merge($alerts_array[0],$singlebtn_array);
But with an array of arrays you will probably want a loop. Not sure if each one would be the same thing or if you will have multiples but this will help you.
$alerts_array; //Your array for arrays
singlebtn_array; //Whatever this is
$flattened_alerts_singlebtn = array();
foreach($alerts_array as $alert_array){
//This will be every array in the alerts as your flattened
//array with the singlebtn
$flattened_alerts_singlebt[] = array_merge($alert_array,$singlebtn_array);
}
You can then loop $flattened_alerts_singlebtn to get each falttened array that could exist.

Get index for multi-dimensional array

I need to get an index of a multidimensional and output it as a variable. I know this is very simple but I am struggling with it.
For example given the following:
[1]=>
array(11) {
["field_label"]=>
string(24) "What is your first name?"
["field_name"]=>
string(6) "f_name"
["identifier"]=>
bool(true)
["options"]=>
bool(false)
["form_name"]=>
string(12) "demographics"
}
[2]=>
array(11) {
["field_label"]=>
string(23) "What is your last name?"
["field_name"]=>
string(6) "l_name"
["identifier"]=>
bool(true)
["options"]=>
bool(false)
["form_name"]=>
string(12) "demographics"
}
[3]=>
array(11) {
["field_label"]=>
string(32) "Researcher who took measurements"
["field_name"]=>
string(17) "weight_researcher"
["identifier"]=>
bool(false)
["options"]=>
bool(false)
["form_name"]=>
string(6) "weight"
}
I want to find the index for the first element that has a form_name of "weight" (#3)
Just use a foreach and an if inside it:
foreach($array as $key => $value) {
// ^ here resides the key of the parent array
if($value['form_name'] == 'weight') { // if form name is weight
echo $key; // echo the key
break; // then stop on first occurence
}
}
PHP >= 5.5.0 needed for array_column:
$key = array_search('weight', array_column($array, 'form_name'));
For multiple keys:
$keys = array_keys(array_column($array, 'form_name'), 'weight');

Push an associate array into another array

I am trying to do a custom WP_query loop and put the results into an array of associate arrays.. It is not working too well.. I realize the array_push overwrites any arrays that have the same indexes so I have the index increment +1 in the loop so they are not identical..however it still is not working.. My results show correct only on the first index (zero).. Here is my code:
<?php
$permlink='permalink';
$excerpt='exerpt';
$title='title';
$id='id';
$finalarray=array();
for ($i = 0; $i <= 10; $i++) {
$newitem = array(array(
'id'.$i =>$id.$i,
'title'.$i => $title.$i,
'excerpt'.$i => $excerpt.$i,
'permalink'.$i => $permlink.$i
));
array_push($finalarray, $newitem);
}
$count=0;
foreach($finalarray as $item){
echo $count.':'.'<br>';
echo $item[$count]['title'.$count];
echo $item[$count]['id'.$count];
echo $item[$count]['permalink'.$count];
$count++;
}
var_dump($finalarray);
?>
Any my results show :
0:
title0id0permalink01:
2:
3:
4:
5:
6:
7:
8:
9:
10:
array(11) { [0]=> array(1) { [0]=> array(4) { ["id0"]=> string(3) "id0" ["title0"]=> string(6) "title0" ["excerpt0"]=> string(7) "exerpt0" ["permalink0"]=> string(10) "permalink0" } } [1]=> array(1) { [0]=> array(4) { ["id1"]=> string(3) "id1" ["title1"]=> string(6) "title1" ["excerpt1"]=> string(7) "exerpt1" ["permalink1"]=> string(10) "permalink1" } } [2]=> array(1) { [0]=> array(4) { ["id2"]=> string(3) "id2" ["title2"]=> string(6) "title2" ["excerpt2"]=> string(7) "exerpt2" ["permalink2"]=> string(10) "permalink2" } } [3]=> array(1) { [0]=> array(4) { ["id3"]=> string(3) "id3" ["title3"]=> string(6) "title3" ["excerpt3"]=> string(7) "exerpt3" ["permalink3"]=> string(10) "permalink3" } } [4]=> array(1) { [0]=> array(4) { ["id4"]=> string(3) "id4" ["title4"]=> string(6) "title4" ["excerpt4"]=> string(7) "exerpt4" ["permalink4"]=> string(10) "permalink4" } } [5]=> array(1) { [0]=> array(4) { ["id5"]=> string(3) "id5" ["title5"]=> string(6) "title5" ["excerpt5"]=> string(7) "exerpt5" ["permalink5"]=> string(10) "permalink5" } } [6]=> array(1) { [0]=> array(4) { ["id6"]=> string(3) "id6" ["title6"]=> string(6) "title6" ["excerpt6"]=> string(7) "exerpt6" ["permalink6"]=> string(10) "permalink6" } } [7]=> array(1) { [0]=> array(4) { ["id7"]=> string(3) "id7" ["title7"]=> string(6) "title7" ["excerpt7"]=> string(7) "exerpt7" ["permalink7"]=> string(10) "permalink7" } } [8]=> array(1) { [0]=> array(4) { ["id8"]=> string(3) "id8" ["title8"]=> string(6) "title8" ["excerpt8"]=> string(7) "exerpt8" ["permalink8"]=> string(10) "permalink8" } } [9]=> array(1) { [0]=> array(4) { ["id9"]=> string(3) "id9" ["title9"]=> string(6) "title9" ["excerpt9"]=> string(7) "exerpt9" ["permalink9"]=> string(10) "permalink9" } } [10]=> array(1) { [0]=> array(4) { ["id10"]=> string(4) "id10" ["title10"]=> string(7) "title10" ["excerpt10"]=> string(8) "exerpt10" ["permalink10"]=> string(11) "permalink10" } } }
So it looks like the values are in the array, however, only the first index ( zero ) prints correctly.. any suggestions? Also, is there any way i can push an associate array and it not be over written so I dont have to increment the index?
There are a few problems here.
for ($i = 0; $i <= 10; $i++) {
$newitem = array(array(
'id'.$i =>$id.$i,
'title'.$i => $title.$i,
'excerpt'.$i => $excerpt.$i,
'permalink'.$i => $permlink.$i
));
array_push($finalarray, $newitem);
}
You are pushing an array, containing an array, containing 4 elements into $finalarray. There is no need for the outer array(). You can just do $newitem = array(...). array_push appends to end of the array, incrementing the index for you.
Second, in your foreach, your $count variable is not needed at all. When using foreach, $item is the element of the array. No need to look it up by the index.
If you want the index, however, foreach can give it to you.
foreach($finalarray as $count=>$item){
echo $count.':'.'<br>';
echo $item['title'.$count];
echo $item['id'.$count];
echo $item['permalink'.$count];
}
Your original code wasn't working because in each $item there was one array. That array had one item. In each element, the inner aray was always at index 0. Incrementing $count made it so only the 1st element worked, the others didn't because $item[1] didn't exist, it was always $item[0].
for ($i = 0; $i <= 10; $i++) {
$newitem = array(array(
'id'.$i =>$id.$i,
'title'.$i => $title.$i,
'excerpt'.$i => $excerpt.$i,
'permalink'.$i => $permlink.$i
));
$finalarray[] = $newitem;
unset($newitem);
}
Since you don't care about the key of the final array just add it on using $finalarray[]

Categories