Getting Value from array - php

To get the value from var_dump:
mymeta_url_group =>
0 => string(39) "a:1:{s:10:"mymeta_url";s:8:"You rock";}"
1 => string(40) "a:1:{s:10:"mymeta_url";s:9:"Yeah Sure";}"
I have used:
$urls= get_post_meta( get_the_ID(), 'mymeta_url_group', false );
foreach ( $urls as $url)
{
echo $url["mymeta_url"];
// You Rock
//Yeah Sure
}
Now since I have added repeater and sorter option in backend new var dump shows:
mymeta_url_group =>
0 => string(102) "a:1:{s:10:"mymeta_url";a:2:{s:11:"cmb-field-0";s:8:"You rock";s:11:"cmb-field-1";s:11:"Nope Maybe ";}}"
1 => string(100) "a:1:{s:10:"mymeta_url";a:2:{s:11:"cmb-field-0";s:9:"Yeah Sure";s:11:"cmb-field-1";s:9:"Won't you";}}"
Now How can I get Values "You rock" "Nope Maybe ""Yeah Sure""Won't you" extending my previous solution.
PS if I do var_dump($url["mymeta_url"]);
The output is
Arrayarray(2) { ["cmb-field-0"]=> string(8) "You rock" ["cmb-field-1"]=> string(11) "Nope Maybe " } Arrayarray(2) { ["cmb-field-0"]=> string(9) "Yeah Sure" ["cmb-field-1"]=> string(9) "Won't you" }

I don't know what get_post_meta() is supposed to do, but maybe this will give you some ideas:
<?php
/* Setting up data */
$data = array (
'a:1:{s:10:"mymeta_url";a:2:{s:11:"cmb-field-0";s:8:"You rock";s:11:"cmb-field-1";s:11:"Nope Maybe ";}}',
'a:1:{s:10:"mymeta_url";a:2:{s:11:"cmb-field-0";s:9:"Yeah Sure";s:11:"cmb-field-1";s:9:"Won\'t you";}}'
);
$urls = array();
foreach ($data as $s) {
$urls[] = unserialize ($s);
}
/* Retrieving data */
foreach ($urls as $url) {
foreach ($url['mymeta_url'] as $u) {
echo "$u, ";
}
echo "\n";
}

Here's what I came up with:
foreach($array as $array2) { //If it doesn't work change $array to $array['mymeta_url_group']
$array2 = unserialize($array2);
foreach($array2['mymeta_url'] as $line) {
echo $line; //$line is the sentance you're looking for
}
}
I'm not sure if will work since I had to reconstruct the array from the var_dump() output.

Related

PHP Associated Arrays only showing first letter of value

I have a HTML form with multiple inputs.
I have the below php code to get them inputs and put them in an associated array.
However, when dumping the Associated array the value only shows the first letter...
<?php
$valueArray=array
(
"servername"=>'',
"serverlocation"=>'',
"servertype"=>'',
"serverdescription"=>''
);
foreach($valueArray as $key => $value)
{
if (isset($_POST[$key]))
{
$postValue = $_POST[$key];
$actualValue = $postValue;
$valueArray[$key][$value] = $actualValue;
}
}
var_dump($valueArray);
?>
This is what is dumped -
array(4) { ["servername"]=> string(1) "d" ["serverlocation"]=> string(1) "K" ["servertype"]=> string(1) "P" ["serverdescription"]=> string(1) "t" } post
How do i get it to store the whole string, and not just the first letter?
If you want to fill the valueArray with the content of the POST request you have to do this:
$valueArray=array
(
"servername"=>'',
"serverlocation"=>'',
"servertype"=>'',
"serverdescription"=>''
);
foreach($valueArray as $key => $value)
{
if (isset($_POST[$key]))
{
$postValue = $_POST[$key];
$valueArray[$key] = $postValue;
}
}
var_dump($valueArray);
I think you ar wrong with this line:
$valueArray[$key][$value] = $actualValue;
Try this
$valueArray=array
(
"servername"=>'',
"serverlocation"=>'',
"servertype"=>'',
"serverdescription"=>''
);
$postData=array
(
"servername"=>'serverName',
"serverlocation"=>'serverLocation',
"servertype"=>'serverType',
"serverdescription"=>'serverDescription'
);
foreach($valueArray as $key => $value)
{
if (isset($postData[$key]))
{
$postValue = $postData[$key];
$actualValue = $postValue;
$valueArray[$key] = $actualValue;
}
}
var_dump($valueArray);

Change value within array based on input in php

I am trying to locale the correct sub-array in order to change the count, if a specific value is present more than once.
I have the following code:
$trending = [];
foreach($hashtags as $hashtag) {
if(in_array($hashtag->hashtag, $hashtags))
{
array_search()
}
else {
array_push($trending, [
'hashtag' => $hashtag->hashtag,
'counts' => '1'
]);
}
}
This gives me the following example outout:
array(3) {
[0]=> array(2)
{
["hashtag"]=> "foobar"
["counts"]=> "1"
}
[1]=> array(2)
{
["hashtag"]=> "hashtags"
["counts"]=> "1"
}
[2]=> array(2)
{
["hashtag"]=> "imageattached"
["counts"]=> "1"
}
}
So in the foreach loop and the if statement, i want to check for dublicates of hashtags, e.g. if the hashtag foobar exists more than one time, I don't want to create another dublicate in the array, but I want to change the count to 2
How do I find the correct "sub"-array, and change the count of this to 2, if a hashtag is present within $hashtags more than once??
The idea is, that I at the end can sort these arrays, and get the hashtag that is most common, by looking at the count.
If you change the structure of your output, you could do something like this:
$trending = [];
foreach($hashtags as $tag) {
if (isset($trending[$tag])) $trending[$tag]++;
else $trending[$tag] = 1;
}
Which would result in $trending having the structure
array(2) {
["foobar"] => 1,
["hashtags"] => 2
}
Which could then be looped through with
foreach($trending as $tag => $count) {
echo $tag . ' appears ' . $count . ' times.' . PHP_EOL;
}
The PHP method array_count_values might be of some help.
http://php.net/manual/en/function.array-count-values.php
Have you considered using a keyed array?
$trending = array();
foreach($hashtags as $hashtag) {
if(!isset($trending[$hashtag])){
$trending[$hashtag] = 1;
}else{
$trending[$hashtag] += 1;
}
}
By using a keyed array, there is no duplication and you can easily check how frequently a hashtag is used by just accessing $trending[$hashtag]. Additionally, you can get the list of all hashtags in the trending array using $allHashtags = array_keys($trending);.
Of course, if your project specifications do not allow for this, then by all means, use a different approach, but that would be the approach I would take.
It can be more linear of you can change your array structure but for the current this should work.
$trending = [];
$checker = true;
foreach($hashtags as $hashtag) {
foreach ($trending as $key =>$value) {
if($value["hashtag"] == $hashtag->hashtag){
$trending[$key]["counts"]++;
$checker = false;
}
}
if($checker) {
array_push($trending, [
'hashtag' => $hashtag->hashtag,
'counts' => '1'
]);
}
$checker = true;
}

How to create a nested array out of an array in PHP

Say, we have an array: array(1,2,3,4,...)
And I want to convert it to:
array(
1=>array(
2=>array(
3=>array(
4=>array()
)
)
)
)
Can anybody help?
Thanks
EDIT It would be good to have the solution with iterations.
$x = count($array) - 1;
$temp = array();
for($i = $x; $i >= 0; $i--)
{
$temp = array($array[$i] => $temp);
}
You can simply make a recursive function :
<?php
function nestArray($myArray)
{
if (empty($myArray))
{
return array();
}
$firstValue = array_shift($myArray);
return array($firstValue => nestArray($myArray));
}
?>
Well, try something like this:
$in = array(1,2,3,4); // Array with incoming params
$res = array(); // Array where we will write result
$t = &$res; // Link to first level
foreach ($in as $k) { // Walk through source array
if (empty($t[$k])) { // Check if current level has required key
$t[$k] = array(); // If does not, create empty array there
$t = &$t[$k]; // And link to it now. So each time it is link to deepest level.
}
}
unset($t); // Drop link to last (most deep) level
var_dump($res);
die();
Output:
array(1) {
[1]=> array(1) {
[2]=> array(1) {
[3]=> array(1) {
[4]=> array(0) {
}
}
}
}
}
I think the syntax for the multidimensional array you want to create would look like the following.
$array = array(
'array1' => array('value' => 'another_value'),
'array2' => array('something', 'something else'),
'array3' => array('value', 'value')
);
Is this what you're looking for?
You can also use this array library to do that in just one line:
$array = Arr::setNestedElement([], '1.2.3.4', 'value');

Spread foreach() results over two columns of a table

I'm trying to spread the results of a foreach() loop over two columns of a table (sorry can't post images as this is my first post to stack overflow). I can see where I'm going wrong but haven't a clue how to correct it. I though maybe a next() instead of repeating the foreach() but can't seem to get that to work. Any help would be greatly appreciated.
I need this:
result1 result2
result3 result4
but I'm getting this:
result1 result1
result2 result2
<table>
<?php
foreach ($tags as $tag) {
echo '<tr><td><input name="taga[]" type="checkbox" value="'.$tag->tag.'" id="'.str_replace(" ", "_", $tag->tag).'_id"';
foreach ($search as $searchword) {
if ($searchword == $tag->tag) echo 'checked="checked"';
}
echo ' /><label for="'.str_replace(" ", "_", $tag->tag).'_id">'.$tag->tag.'</label></td>';
next ($tag);
echo '<td><input name="taga[]" type="checkbox" value="'.$tag->tag.'" id="'.str_replace(" ", "_", $tag->tag).'_id"';
foreach ($search as $searchword) {
if ($searchword == $tag->tag) echo 'checked="checked"';
}
echo ' /><label for="'.str_replace(" ", "_", $tag- >tag).'_id">'.$tag->tag.'</label></td></tr>';
}
?>
</table>
You should try to simplify your code when debugging. This would help you isolate the problem and make it easier for communities like StaceOverflow to help you. It looks like this:
<?php
// some fixtures for us to work with
$tags = array('foo', 'bar', 'lolo', );
?>
<pre>
<?php
// your actual issue
foreach ($tags as $tag) {
var_dump( $tag );
next( $tag );
var_dump( $tag );
}
?>
</pre>
You'd see this (wrong) output, which is pretty much the problem you're talking about isn't it ? ;)
string(3) "foo"
string(3) "foo"
string(3) "bar"
string(3) "bar"
string(4) "lolo"
string(4) "lolo"
The signature of next() is:
mixed next ( array &$array )
Which means that it returns a value of any type from an array which is passed by reference. In your case it applies like this:
$tag = next( $tags );
But what happens if you call next() on the last item ?
This function may return Boolean FALSE, but may also return a
non-Boolean value which evaluates to FALSE. Please read the section on
Booleans for more information. Use the === operator for testing the
return value of this function.
You should break the for loop if next returns false, e.g.:
$tag = next( $tags );
if ( $tag === false ) break;
More elaborated example:
<?php
class Tag {
public $tag;
public function __construct( $tag ) {
$this->tag = $tag;
}
}
$tags = array( );
foreach ( array('foo', 'bar', 'lolo', ) as $word) {
$tags[] = new Tag( $word );
}
$search = array( 'bar', 'the' );
?>
<pre>
<?php
foreach ($tags as $tag) {
var_dump( $tag );
$tag = next( $tags );
if ( $tag === false ) {
break;
}
var_dump( $tag );
}
?>
</pre>
Will output just fine:
object(Tag)#1 (1) {
["tag"]=>
string(3) "foo"
}
object(Tag)#3 (1) {
["tag"]=>
string(4) "lolo"
}
object(Tag)#2 (1) {
["tag"]=>
string(3) "bar"
}
Here's the solution applied to your code (tested/working):
<table>
<?php
foreach ($tags as $tag) {
echo '<tr><td><input name="taga[]" type="checkbox" value="'.$tag->tag.'" id="'.str_replace(" ", "_", $tag->tag).'_id"';
foreach ($search as $searchword) {
if ($searchword == $tag->tag) echo 'checked="checked"';
}
echo ' /><label for="'.str_replace(" ", "_", $tag->tag).'_id">'.$tag->tag.'</label></td>';
$tag = next ($tags);
if ( $tag === false ) {
break;
}
echo '<td><input name="taga[]" type="checkbox" value="'.$tag->tag.'" id="'.str_replace(" ", "_", $tag->tag).'_id"';
foreach ($search as $searchword) {
if ($searchword == $tag->tag) echo 'checked="checked"';
}
echo ' /><label for="'.str_replace(" ", "_", $tag->tag).'_id">'.$tag->tag.'</label></td></tr>';
}
?>
</table>
The php manual for next also includes an important note:
Note: You won't be able to distinguish the end of an array from a
boolean FALSE element. To properly traverse an array which may contain
FALSE elements, see the each() function.
If both columns are exactly the same, or especially if you need more than 2, I recommend this condensed approach (I haven't done a lot of testing on this):
<table>
<tr>
<?php
$count = 0;
$columnCount = 2;
foreach ($interests as $interest) {
if($count++ % $columnCount == 0){
echo "</tr><tr>";
}
echo "<td>$interest->name</td>";
}
while($count++ % $columnCount != 0){
echo "<td> </td>";
}
?>
</tr>
</table>
It will not handle an empty array correctly, so you should add your own code to handle that. You can change columnCount to whatever you want, if you want more than 2 columns.

Compare two different array keys, then do something with the values that dont exist in the 1st array

Arrays seem to be one of those subjects that just doesn't want to click for me. Seemed easier in SQL... I do hope you can help..
I have two arrays that I wish to compare the keys layouts are completely different, I wish to compare *$first_array->place->name* with *$second_array->post_name* then if a value from the first array doesn't exist in the second then I want to do something about that, probably an Insert statement of sort..
$first_array=simplexml_load_file('xml/file.xml'); // Load an XML file
echo "<p>Check we are getting a value from the 1st array: ".$first_array->place[0]->name."</p>";
//echo"<pre>";
//var_dump($first_array);
//echo"</pre>";
$second_array=get_pages(); // Get a list of Posts from WP
echo "<p>Check we are getting a value from the 2nd array: ".$second_array[0]->post_name."</p>";
//echo"<pre>";
//var_dump($second_array);
//echo"</pre>";
From the First Array..
object(SimpleXMLElement)#99 (1) {
["place"]=>
array(5) {
[0]=>
object(SimpleXMLElement)#101 (14) {
["name"]=>
string(5) "China"
From the Second Array..
array(6) {
[0]=>
object(stdClass)#199 (24) {
["post_title"]=>
string(5) "Japan"
more
[1]=>
object(stdClass)#197 (24) {
["post_title"]=>
string(5) "China"
more
[2]=>
object(stdClass)#197 (24) {
["post_title"]=>
string(6) "Israel"
The difference obviously being Japan, and Israel
Progress update # 1
I got the the stage where I have two results for the records that DO exist, but I want a distinct list of the records that exist in the XML file, but DO NOT exist in the $second_array:
<?php
$xml = simplexml_load_file('xml/regions.xml');
// echo "<p>".$xml->place[0]->name."</p>";
echo"<pre>";
var_dump($xml);
echo"</pre>";
$args = array('post_type' => 'page','child_of' => 20,'exclude' => 22);
$wparray=get_pages($args);
// echo "<p>".$wparray[0]->post_name."</p>";
echo"<pre>";
var_dump($wparray);
echo"</pre>";
foreach($xml as $yregions_places) {
for($j=0;$j<count($wparray);$j++) {
if($yregions_places->name==$wparray[$j]->post_title) {
echo "<p style=\"color:green;\">".$yregions_places->name."<p>"; }
}
}
?>
Can someone help me get to the next stage? I really am trying!
Progress update # 2
After Brian's pointer I have managed I think I have both sets of data in an array:
$yregions_xml = (array)simplexml_load_file('xml/regions.xml');
// echo"<pre>";
// var_dump($yregions_xml);
// echo"</pre>";
$yregions_xml = array_pop($yregions_xml);
for($j=0;$j<count($yregions_xml);$j++) {
echo "<p style=\"color:purple;\">".(trim(strtolower($yregions_xml[$j]->name)))."</p>";
}
$wpargs = array('post_type' => 'page','child_of' => 20,'exclude' => 22);
$wparray=get_pages($wpargs);
// echo"<pre>";
// var_dump($wparray);
// echo"</pre>";
for($j=0;$j<count($wparray);$j++) {
echo "<p style=\"color:green;\">".(trim(strtolower($wparray[$j]->post_name)))."</p>";
So not I have to do some comparison?
Progress update # 3
//store XML data in SimpleXMLObject
$xml = simplexml_load_file('xml/file.xml');
$first_array=array_pop($yregions_xml);
//initiate arrays
$first_array = array();
$second_array = array();
//populate arrays with object data that interests us
foreach($xml->place as $place){
foreach($place as $name){
$first_array[] = $name;
echo "<p style=\"color:green;\">".$name."</p>";
}
}
$wpargs = array('post_type' => 'page','child_of' => 20,'exclude' => 22);
foreach(get_pages($wpargs) as $page){
$second_array[] = $page->post_title;
echo "<p style=\"color:red;\">".$page->post_title."</p>";
}
//perform comparison
$unique_to_first_array = array_diff($first_array,$second_array);
echo "<pre>";
var_dump($unique_to_first_array);
echo "</pre>";
//now do your SQL, etc with this new array
Completed Code
I left all the progress in there, so everyone could see the work in progress, hopefully that will help others more. I know I have struggled with this. Thank you #brian_d and #rrapuya for your full and commented answers of assistance.
// store XML data in SimpleXMLObject
$xml = simplexml_load_file('xml/file.xml');
// initiate arrays
$first_array = array();
$second_array = array();
// populate arrays with object data that interests us
foreach($xml->place as $place){
foreach($place->name as $name){
$first_array[] = $name;
// echo "<p style=\"color:green;\">".$name."</p>";
}
}
$wpargs = array('post_type' => 'page','child_of' => 20,'exclude' => 22);
foreach(get_pages($wpargs) as $page){
$second_array[] = $page->post_title;
// echo "<p style=\"color:red;\">".$page->post_title."</p>";
}
// perform comparison
$unique_to_first_array = array_diff($first_array,$second_array);
echo "<pre>";
var_dump($unique_to_first_array);
echo "</pre>";
//now do your SQL, etc with this new array
I hope this helps. The two key points are populating your arrays from the object data and then using the PHP function array_diff to get values found only in the first array.
//store XML data in SimpleXMLObject
$xml = simplexml_load_file('xml/file.xml');
//initiate arrays
$first_array = array();
$second_array = array();
//populate arrays with object data that interests us
foreach($xml->place as $place){
foreach($place as $name){
$first_array[] = (string)$name;
}
}
foreach(get_pages() as $page){
$second_array[] = $page->post_title;
}
//perform comparision
$unique_to_first_array = array_diff($first_array, $second_array);
//now do your SQL, etc with this new array
try this foreach ($first_array as $arrobj ){
for($j=0;$j<count($second_array);$j++){
if($arrobj->place->name ==$second_array[$j]->post_title){
//do query here
}else{
//do query here
}
}
}

Categories