Solved

Adjust PHP Array

Posted on 2014-04-15
14
332 Views
Last Modified: 2014-04-16
Okay so i have this $array output

Array
(
    [0] => Array
        (
            [0] => 8820
        )

    [1] => Array
        (
            [0] => 102
        )

    [2] => Array
        (
            [0] => 100971
        )

    [3] => Array
        (
            [0] => 5723
        )

    [4] => Array
        (
            [0] => 608898
        )

    [5] => Array
        (
            [0] => 39294
        )

    [6] => Array
        (
            [0] => 4743
        )



how do i adjust this so its new output would be:

Array
        (
            [0] => 8820
            [1] => 102
            [2] => 100971
            [3] => 5723
           ..... and so on

        )
0
Comment
Question by:jporter80
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 3
  • +1
14 Comments
 
LVL 35

Expert Comment

by:Dan Craciun
ID: 40003165
Can you post your code, please?
0
 

Author Comment

by:jporter80
ID: 40003168
while ( $q->have_posts() ) { $q->the_post();
            $u[] = get_post_custom_values('_u');
      }

print_r($u);
0
 
LVL 35

Expert Comment

by:Dan Craciun
ID: 40003175
while ( $q->have_posts() ) { $q->the_post();
            $u[] = get_post_custom_values('_u');
      }
$1dArray=array();
$i = 0;
foreach ($u as $key => $value) {
    $1dArray[$i] = $value;
    $i++;
}

print_r($1dArray);
0
Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

 

Author Comment

by:jporter80
ID: 40003184
that gives me the same thing:

Array
(
    [0] => Array
        (
            [0] => 8820
        )

    [1] => Array
        (
            [0] => 102
        )

    [2] => Array
        (
            [0] => 100971
        )

    [3] => Array
        (
            [0] => 5723
        )

    [4] => Array
        (
            [0] => 608898
        )

    [5] => Array
        (
            [0] => 39294
        )

    [6] => Array
        (
            [0] => 4743
        )

    [7] => Array
        (
            [0] => 439
        )

    [8] => Array
        (
            [0] => 500965
        )
0
 

Author Comment

by:jporter80
ID: 40003190
got it...

while ( $q->have_posts() ) { $q->the_post();
            $u[] = get_post_custom_values('_u');
      }
$dArray=array();
foreach ($u as $key => $value) {
    $dArray[$i] = $value[0];
}

print_r($dArray);

not sure if that was the best way.. but it works.
0
 
LVL 58

Accepted Solution

by:
Julian Hansen earned 500 total points
ID: 40003364
No need to loop through the data twice - just use array_shift on the get_post_custom_values to get the first element in the array to add to your $u array.
while ( $q->have_posts() ) { $q->the_post();
    $u[] = array_shift(get_post_custom_values('_u'));
}

print_r($u); 

Open in new window

0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 40003643
Absent a very large number of posts, repeated iteration won't matter very much.  This is one of those things that is hard to find after optimizing, so there is no reason to optimize the code.  But that said, array_shift() is almost certainly the right tool, if you need a tool.  It would be interesting to see if get_post_custom_values('_u') could be addressed with something like get_post_custom_values('_u')[0] which seems to be the data element we want - the function returns an array and we only want the zero-element.

This is almost certainly wrong, since it injects a time-bomb into the code:
foreach ($u as $key => $value) {
    $dArray[$i] = $value[0];
}

Open in new window

The time-bomb is the variable $i.  Where is this set?  Where is it incremented?  Without knowing that, you can't really know what is going to show up in $dArray, and if another piece of code mutates $i you have the potential for a runtime error -- a real mess.  If you omit $i from $dArray[$i] = $value[0]; and instead write $dArray[] = $value[0]; you remove this potential for error.

PHP arrays are among the most misunderstood data structures.  It's worth reading the online man page for understanding.
http://php.net/manual/en/book.array.php
http://php.net/manual/en/language.types.array.php
http://php.net/manual/en/language.operators.array.php
http://php.net/manual/en/function.array.php
http://php.net/manual/en/ref.array.php
http://php.net/manual/en/array.constants.php
0
 
LVL 58

Expert Comment

by:Julian Hansen
ID: 40003850
Absent a very large number of posts, repeated iteration won't matter very much
It won't but this leads to lazy programming which in turn leads to bloat an inefficiency.

Irrespective of the marginal gain - there is always merit in writing the most optimal code. In thise case the optimal solution is also the simplest as it
a) Eliminates several lines of unnecessary code
b) Performs what we would intuitively expect looking at the data.

I would be very wary of following advice that suggests one should not give consideration to writing optimal code.

The $i in the context of the last post is not really relevant here

get_post_custom_values('_u')[0] will only work in 5.4+
< 5.3 requires either array_shift or an intemediary variable. As the version was unknown here I went with what was most backwardly compatible.
0
 

Author Comment

by:jporter80
ID: 40003872
Thanks for the info guys!! It was actually a bad copy and paste.  I dont have the $i in the code im using.

Corrected

while ( $q->have_posts() ) { $q->the_post();
            $u[] = get_post_custom_values('_u');
      }
$dArray=array();
foreach ($u as $key => $value) {
    $dArray[] = $value[0];
}

Open in new window


the array will contain probably about 20-50 keys at one time.  Would arrayshift() be best in this situation?
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 40003882
@julianH: I generally agree with this statement:
always merit in writing the most optimal code
however that assumes that one knows how to write the most optimal code the first time.  If you take ten minutes to make a test for the most optimal code and your optimization gains you a couple of microseconds, you will have to run the code three hundred million times before you recover the ten minutes you spent on the problem.
http://xkcd.com/1205/
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 40003903
For better or worse, computer programming is a process that requires exact and meticulous attention to detail.  Errant copy/paste might confuse us (no big deal), and it might also make your scripts fail.  Better to cultivate habits that lead to accuracy.
Would arrayshift() be best...
The function is array_shift() and as julianH points out, you could either choose that or direct [0] notation, depending on the version of PHP.   You can find the version of PHP by running this function: phpinfo().  If you have a sense of humor, set up a test to compare these different programming methods and see which is faster :-)
0
 
LVL 58

Expert Comment

by:Julian Hansen
ID: 40003921
This would be the best way forward in my opinion

while ( $q->have_posts() ) { $q->the_post();
    $u[] = array_shift(get_post_custom_values('_u'));
}

print_r($u); 

Open in new window

So yes array_shift is the best answer here. In addition to being backwardly compatible in my view the [0] is not as clear as array_shift in what you are doing. They perform essentially the same thing so I would go with the array_shift.

As for optomising code.

There is a big difference between

while( ... ) {
   ....
}
foreach() {
  ...
}

Open in new window

and just
while( ... ) {
 ...
}

Open in new window

I can't think of any scenario where there is a valid argument for the former.
0
 

Author Closing Comment

by:jporter80
ID: 40003952
thanks!  works well.
0
 
LVL 58

Expert Comment

by:Julian Hansen
ID: 40004000
You are welcome - thanks for the points.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The purpose of this video is to demonstrate how to Import and export files in WordPress. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: mywebsite.com/wp-login.php : Click on Too…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question