1

I stuck with a problem: I have an array with IDs and want to assign theses IDs to a key of a associative array:

$newlinkcats = array( 'link_id' => $linkcatarray[0], $linkcatarray[1], $linkcatarray[2]);

this works fine, but I don't know how many entries in $linkcatarray. So I would like to loop or similar. But I don't know how.

  • no push, cause it is no array
  • no implode, cause it is no string
  • no =, cause it overrides the value before

Could anyone help?

Thanks Jim

2
  • 1
    You can just add the whole array. $newlinkcats = array('link_id' => $linkcatarray); Commented Mar 14, 2014 at 17:09
  • What your question is currently asking for seems to be impossible. I think you need to explain it better, provide an accurate description of the source data, and better explain what you want the end result to be. Commented Mar 14, 2014 at 17:11

2 Answers 2

1

Why not just implode it ?

$newlinkcats = array(
    'link_id' => implode(
        ',',
        $linkcatarray
    )
);

Or just do this:

// Suggested by Tularis
$newlinkcats = array(
    'link_id' => $linkcatarray
);
Sign up to request clarification or add additional context in comments.

1 Comment

or alternatively just add the entire array: $newlinkcats = array('link_id'=>$linkcatarray);
0

If your $linkcatarray array is only comprised of the values you wish to assign to the link_id key, then you can simply point the key at that array:

$newlinkcats = array('link_id' => $linkcatarray);

If that array contains more values that you don't want included, then take a look at array_slice() to only grab the indexes you need:

// Grabs the first 3 values from $linkcatarray
$newlinkcats = array('link_id' => array_slice($linkcatarray, 0, 3));

If your desired indexes aren't contiguous, it may be easier to cherry-pick them and use a new array:

$newlinkcats = array('link_id' => array(
    $linkcatarray[7],
    $linkcatarray[13],
    $linkcatarray[22],
    // ...
));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.