• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 335
  • Last Modified:

Adjust PHP Array

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
jporter80
Asked:
jporter80
  • 5
  • 4
  • 3
  • +1
1 Solution
 
Dan CraciunIT ConsultantCommented:
Can you post your code, please?
0
 
jporter80Author Commented:
while ( $q->have_posts() ) { $q->the_post();
            $u[] = get_post_custom_values('_u');
      }

print_r($u);
0
 
Dan CraciunIT ConsultantCommented:
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
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
jporter80Author Commented:
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
 
jporter80Author Commented:
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
 
Julian HansenCommented:
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
 
Ray PaseurCommented:
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
 
Julian HansenCommented:
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
 
jporter80Author Commented:
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
 
Ray PaseurCommented:
@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
 
Ray PaseurCommented:
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
 
Julian HansenCommented:
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
 
jporter80Author Commented:
thanks!  works well.
0
 
Julian HansenCommented:
You are welcome - thanks for the points.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 5
  • 4
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now